DO - WHILE loop

The syntax of a DO - WHILE loop is as follows:

DO
    { statements1; }
WHILE expression
    { statements2; }
END

The loop first executes the first set of statements, statements1, before evaluating expression. If expression evaluates to TRUE, then the loop continues on and executes the second set of statements, statements2, before returning to the start of the loop. If expression evaluates to FALSE then the loop exits without executing statements2.

This means that statements1 will always be executed at least once. Either set of statements may be empty: the WHILE clause may immediately follow the DO keyword or immediately precede the END keyword.

The loop can be terminated early by using the BREAK statement. This causes execution to move to the statement after the END of the loop.

The DO - WHILE loop is identical to the DO - UNTIL loop, except that the sense of expression is inverted.

Examples

In the following example, the function Func(b) will be executed at least once, but the value of b on exit will be 10, because the second statement is skipped the last time around the loop.

DO
  Func(b)();
WHILE b != 10
  b := b + 1;
END;

To implement a simple WHILE loop (similar to the C while loop), the following is used:

DO WHILE b != 10
  b := b + 1;
END;

To implement a C style do while loop:

DO
  b := b + 1;
WHILE b < 10
END;