Do…Loop Statement

Runs 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 runs statements until condition becomes false.
Mandatory unless you use While. The loop runs statements until condition becomes true.
Mandatory Boolean expression.
Optional one or more statements between Do and Loop. These run while condition is true, or until condition becomes true.
The alternative is two or more groups of statements. The first group starts with Exit When, and runs the same as specified above. The subsequent groups start with When, and run 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 run one or more statements again and again. It is recommended when you cannot be sure how many times the statements must run. 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 run the statements a known number of times, the statement For can give better performance.

Usual pre-conditional operation


Usual post-conditional operation


Exit Do

The statement Exit Do can stop the operation 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 run because condition was initially false, use the statement When NONE.

The post-condition construct does not let you use When NONE. The statements always run a minimum of one time, because the condition comes last.

See Exit When Clause for more information.


Do While
Loop While

See also