Executes a sequence of statements while a given condition is true.
While condition [ statements ] End [ While ]
- Mandatory expression of the type
- Optional one or more executable statements.
They execute while
- Alternatively, 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 after the end of the loop. See
Whenstatements below for more information.
- Completes the statement.
You can also use
- You can change this part of the syntax.
@OptionDirective for more information.
End While is not the same as
Use the construct
While 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
Other loop constructs are possibly more flexible or can give better performance.
See the See also section below for more information.
condition is true, then
End, control moves back to
condition is calculated again.
condition stays true, then
statements continue to execute.
If it becomes false, then control moves to the statement after
While always calculates the
condition is initially false, then the
statements do not execute.
Exit While can stop
Exit While immediately moves control to the statement after
To find if
While completed because
condition became false, use
To find if
While was not executed because
condition was initially false, use
Begin…When Statements for more information.
Dim counter = 5 Dim factorial = 1 While counter > 0 factorial *= counter Counter -= 1 End While