Visual Basic Language Reference  

While...End While Statements

Executes a series of statements as long as a given condition is True.

While condition
   [ statements ]
End While

Parts

condition
Required. Expression. Must evaluate to True or False. If condition is Nothing, condition is treated as False.
statements
Optional. One or more statements following While that are executed while condition is True.
End While
Terminates execution of the While block.

Remarks

If condition is True, all of the statements are executed until the End While statement is encountered. Control then returns to the While statement and condition is again checked. If condition is still True, the process is repeated. If it is False, execution resumes with the statement following the End While statement.

You can nest While loops by placing one loop within another.

Example

This example uses the While...End While statement to increment a counter variable. The statements in the loop are executed as long as the condition evaluates to True.

Dim Counter As Integer = 0
While Counter < 20   ' Test value of Counter.
   Counter += 1   ' Increment Counter.
End While   ' End While loop when Counter > 19.
Debug.WriteLine (Counter)   ' Prints 20 in the Output window.

See Also

Do...Loop Statements | Do...Loop Statements (Conceptual) | With...End With Statements | While Statement (Conceptual)