While Statement

Runs a sequence of statements while a given condition is true.


While condition
    [ statements ]
End [ While ]


Mandatory Boolean expression.
Optional one or more statements. They run while condition is true.
The alternative is two or more blocks of statements. The first block starts with Begin, and runs the same as given above. The subsequent blocks start with When, and run after the loop stops. See When statements below for more information.
Completes the statement. You can also use End While.
You can change this part of the syntax. See @Option Directive for more information.


End While is not the same as Exit While.

Use the construct While 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 after While. Other loop constructs are possibly more flexible or can give better performance. See the See also section below for more information.

Usual operation

If condition is true, statements run. After End, control moves back to While, where condition calculates again. While condition stays true, statements continue to run. But when it becomes false, control moves to the statement after End.

condition always calculates before statements run. But if condition is initially false, statements do not run.

Exit While

The statement Exit While can stop While. Exit While immediately moves control to the statement after End.

When statements

To find if While completed because condition became false, use When DONE.

To find if While was not run because condition was initially false, use When NONE.

See Exit When Clause for more information.


Dim counter = 5
Dim factorial = 1

While counter > 0
    factorial *= counter
    Counter -= 1
End While

See also