While...End While Statements
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

While...End While Statements

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

While condition
   [ statements ]
End While


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


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.


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)

© 2015 Microsoft