DO [ WHILE Condition ] . . . [ BREAK | CONTINUE ] . . . LOOP [ UNTIL Condition ]
Repeats a number of statements while the initial condition remains true or until the final condition becomes true.
Part | Description |
---|---|
DO | Always the first statement of the loop. |
WHILE | If used, states a Condition that must remain true to execute the loop. |
UNTIL | If used, states a Condition that has to become true to stop execution of the loop. |
Condition | Any boolean expression. |
BREAK | Immediately jumps out of the loop and continues execution of the program with the next line after the loop. |
CONTINUE | Immediately leaves out all following statements in the loop and jumps to the end of the loop causing it to start all over again. |
LOOP | Always the last statement of the loop. |
![]() | If the initial Condition is false to begin with, the loop is not executed at all. Otherwise, the loop will be executed at least once, even if the final Condition is true to begin with. |
' A very simple loop a = 1 DO WHILE a <= 5 PRINT "Hello World"; a INC a LOOP <hr>Hello World 1 Hello World 2 Hello World 3 Hello World 4 Hello World 5
' The same effect with UNTIL DO PRINT "Hello World"; a INC a LOOP UNTIL a = 6
![]() | Be careful not to enter the UNTIL loop with "a" being larger than 5. "a" will be incremented to a larger value than 6, and the only way to stop the loop is lost. You might be better off to use "LOOP UNTIL a > 5" instead to minimize the risk of infinite loops. |
' This loop will never reach its end value a = 1 DO WHILE a <= 5 PRINT "Hello World"; a INC a IF a = 4 THEN BREAK LOOP <hr>Hello World 1 Hello World 2 Hello World 3