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
- 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
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 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
Other loop constructs are possibly more flexible or can give better performance.
See the See also section below for more information.
condition is true,
End, control moves back to
condition calculates again.
condition stays true,
statements continue to run.
But when it becomes false, control moves to the statement after
condition always calculates before
condition is initially false,
statements do not run.
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
Exit When Clause for more information.
Dim counter = 5 Dim factorial = 1 While counter > 0 factorial *= counter Counter -= 1 End While