# While Statement

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

## Syntax

``````While condition
[ statements ]
End [ While ]
``````

## Parts

condition
Necessary – a Boolean expression
statements
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 cannot start, or after it completes. See the When statements section for more information.
End
Completes the statement. You can also use EndWhile.
You can change this part of the syntax. See @Option Directive for more information.

## Instructions

Notice: The End While part is not the same as Exit While.

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

### Usual execution

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.

### Exit While

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

### When statements

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 information.

## Examples

``````Dim counter = 5
Dim factorial = 1

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