Do…Loop Statement

Executes a sequence of statements while a given condition is true, or until a condition becomes true.

Syntax

Pre-condition construct

Do { While | Until } condition
    [ statements ]
Loop

Post-condition construct

Do
    [ statements ]
Loop { While | Until } condition

Parts

Do
Necessary start of the statement.
While
Necessary unless you use Until. The loop executes statements until condition becomes false.
Until
Necessary unless you use While. The loop executes statements until condition becomes true.
condition
Necessary Boolean expression.
statements
Optional one or more statements between Do and Loop. These execute while condition is true, or until condition becomes true.
Alternatively, two or more groups of statements. The first group starts with Begin, and executes the same as specified above. The subsequent groups start with When, and execute when (1) the statements after Begin cannot start, or (2) after they complete. See the When statements section for more information.
Loop
Completes the statement.

Instructions

Use Do…Loop when you must execute one or more statements again and again. It is recommended when you cannot be sure how many times the statements must execute. Control depends on the Boolean condition that you supply. The pre-condition construct is equivalent to the statement While, but Do…Loop is more flexible. If you must execute the statements a known number of times, the statement For can give better performance.

Usual pre-conditional execution

TODO

Usual post-conditional execution

TODO

Exit Do

The statement Exit Do can stop the execution of Do…Loop. Exit Do immediately moves control to the statement after Loop.

When statements

To find if a loop with Do While completed because condition became false, use the statement When %Done.

To find if a loop with Do While was not executed because condition was initially false, then use the statement When %None.

The post-condition construct does not permit you to use When %None. The statements always execute a minimum of one time, because the condition comes last.

See Begin…When Statements for more information.

Examples

Do While
Loop
Do
Loop While

See also