These instructions bound a block of instructions that are repeatedly executed so long as a specified expression evaluates to True.
While condition
[statements]
End While
Prerequisites
None
Parameters
condition
Required expression that is interpreted as a True or False value. Any expression that yields a numeric result can be specified, not just Boolean expressions. Any expression that evaluates to <>0 is interpreted as a True condition.
statements
Optional statement or list of statements that are repeatedly executed so long as the condition evaluates to True.
Remarks
This control structure tests an expression and repeatedly executes a block of statements. It can be used to implement program instruction loops.
The While statement begins execution by testing the value of the condition. If the condition is True, the statements are executed. When the End While instruction is encountered, the condition is tested again. If the condition is still True, the statements are executed once again. This process is repeated until the condition tests False or the statements explicitly execute an instruction that continues execution outside of the loop. If the condition ever tests False, execution continues at the instruction following the End While.
If the condition is False when the While first begins execution, the statements are skipped, in which case, the statements are not executed even once.
For more complex logic, multiple While…End While sequences can be nested to an arbitrary depth and can be combined with other nested control structures. For example, a While loop can contain an If…Then…End If sequence which can in turn contain another While…End While sequence.
Execution of the While loop can be terminated by a number of different methods: the condition can be set False prior to the execution of the End While statement; execution can be explicitly transferred to an instruction outside of the loop, e.g. by the execution of a GoTo instruction; or an Exit While instruction can be executed.
When an Exit While statement is encountered, execution of the innermost While…End While sequence is immediately terminated and execution continues at the instruction following the End While. There can be none or several Exit While statements within each While loop.
Examples
Dim count As Integer
count = 10
While count > 0 'This condition initially evaluates to True
If count = 5 Then
Exit While ' Prematurely stops While loop
End If
count -= 1 ' Same as “count = count-1”
End While
See Also
Statements |Do… Loop Statements| For…Next Statements | GoTo Statements | If…Then…Else…End If Statements