Do...Loop Statement

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


Preconditional form

Do { While | Until } condition
    [ statements ]

Postconditional form

    [ statements ]
Loop { While | Until } condition


Necessary – Starts the statement.
Necessary, unless Until is used – Execute the statements until condition becomes false.
Necessary, unless While is used – Execute the statements until condition becomes true.
Necessary – A Boolean expression
Optional – One or more statements between Do and Loop. These execute while condition is true, or until condition becomes true.
Alternatively, two or three groups of statements. The first group starts with Begin, and executes the same as specified above. The subsequent groups start with When, and execute either when the Begin group cannot start, or after it completes. See the When statements section for more information.
Completes the Do...Loop statement.


Use the Do...Loop structure 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 specify. The preconditional form is equivalent to the While statement, but Do...Loop is more flexible. If you must execute the statements a known number of times, the For statement can give better performance.

Usual preconditional execution


Usual postconditional execution


Exit Do

The Exit Do statement can stop a Do...Loop statement. Exit Do immediately moves control to the statement after Loop.

When statements

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

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

The postconditional form does not permit the use of When %None. The statements always execute at least one time, because the condition comes last.

See Begin...When Statements for more information.


Do While
Loop While

See also