Do…Loop Statement

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


Pre-condition construct

Do { While | Until } condition
    [ statements ]

Post-condition construct

    [ statements ]
Loop { While | Until } condition


Mandatory start of the statement.
Mandatory unless you use Until. The loop executes statements until condition becomes false.
Mandatory unless you use While. The loop executes statements until condition becomes true.
Mandatory Boolean expression.
Optional one or more statements between Do and Loop. These execute while condition is true, or until condition becomes true.
The alternative is 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.
Completes the statement.


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 is connected with 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, then the statement For can give better performance.

Usual pre-conditional execution


Usual post-conditional execution


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 let you use When None. The statements always execute a minimum of one time, because the condition comes last.

See Begin…When Statements for more information.


Do While
Loop While

See also