Executes a sequence of statements while a given condition is true.
While condition [ statements ] End [ While ]
- Required — a Boolean expression
- Optional — One or more statements between While and End. These execute each time that condition is 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 can not start, or after it completes. See the When statements section for more details.
- Completes the While statement. Optionally, you can use EndWhile.
Notice: The End While line is not the same as the Exit While statement.
Use the While structure when you must execute one or more statements again and again. It is recommended when you can not be sure how many times the statements must execute. Control depends on the Boolean condition that you specify in the While line. Other structures are possibly more flexible or can give better performance. See the See also section below for details.
If condition is true, the statements execute. After the End While line, control moves back to the While line, where the condition is recalculated. If the condition stays true, the statements continue to execute. If it becomes false, control moves to the statement after End While.
The While line always calculates the condition before any statements execute. If the condition is initially false, the statements do not execute at all.
The Exit While statement can stop a While statement. Exit While immediately moves control to the statement after End While.
To find if a While statement completed because the condition became false, use the When %Done statement.
To find if a While statement was not executed because the condition was initially false, use the When %None statement.
See Begin...When Statements for more details.
Dim counter = 5 Dim factorial = 1 While counter > 0 factorial *= counter Counter -= 1 End While