while( test )
Parameters
| Type | Description | |
| test | boolean | expression to check for true |
_result
Not set.
Descriptions
The while statement is not actually a robot command, but rather a way to repeatedly run a block of instructions. A while line marks the beginning of a block of instructions. The block of instructions that is started by a while line must be closed with an endw line. While statements may be nested.
The while statement creates a loop the is repeated until the test expression evaluates to false. When test evaluates to true, the block of code between the while and endw lines is run. The loop is then restarted by returning to the while line for reevaluation of the test expression. When test evaluates to false, the block of code between the while and endw lines is skipped and the loop ends.
Since a while loop will continue to repeat until test evaluates to false, the code block of a while statement usually changes the value of a variable in the test expression. After a certain number of loops, these changes should cause test to become false thereby ending the loop.
In the example below, the names of a robot's team members are printed. First a variable named idx is intialized to 0. The loop repeats itself until the value of idx is greater than or equal to the total number of team members. Each time the while statement's code block is run, it prints a team member's name and then increases the value of idx.
See Also