Gambas Documentation - /lang/do

/lang/do

# DO

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. If neither WHILE nor UNTIL is used, the loop becomes an endless loop that can only be exited via the BREAK statement. If the break statement is missing in such a case, it becomes an infinite loop, a condition that is almost universally undesirable.

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 on a WHILE statement 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 on an UNTIL statement is true to begin with.

### Examples

    ' A very simple loop

    a = 1

    DO WHILE a <= 5
      PRINT "Hello World"; a
      INC a
    LOOP

    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 a > 5" instead to minimize the risk of infinite loops.

### Examples

    ' 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

    Hello World 1
    Hello World 2
    Hello World 3

### See also

- Loop Control Structures