LOOP <counter> FROM <start> TO <end> [BY <step>]
- The variable that acts as your loop counter
- The starting integer or float value for the loop counter
- The ending integer or float value for the loop counter
- (optional) The integer or float amount by which to modify the counter on each iteration. If not included, the automatic step value is 1.
This is a conditional loop which will repeat the commands within the loop structure multiple times, depending on the start, end, and step values that are set.
To break out of a loop, use the BREAK command.
When the system finishes running all of the commands within the loop structure, the counter will be incremented or decremented as appropriate, and the loop code will start over. To speed up this process without running all of the commands in the structure, use the CONTINUE command.
For a different kind of logical loop structure, please see the WHILE command.
This will print the numbers 1 through 10, and then stop.
loop v from 1 to 10 MsgArea( v ) // v is autoconverted to a string .
This loop counts down from 10 to 1, by 2s.
loop v from 10 to 1 by -2 MsgArea( v ) .
This loop does the same thing, but in a different way:
loop v from 20 to -5 by -2 if (v > 10) continue // skip the remaining lines and just loop again . MsgArea( v ) if (v <= 1) // Don't wait for the counter to run out, stop now. break . .
Note that when looping by a float value, the imprecision of floats must be taken into account. For example, this loop:
loop v from 1 to 2 by 0.1 MsgArea( v ) .
would produce output that looked like this:
1.0 1.10000002384 1.20000004768 1.30000007153 1.40000009537 1.50000011921 1.60000014305 1.70000016689 1.80000019073 1.90000021458