|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 (C# Reference)
The while statement executes a statement or a block of statements until a specified expression evaluates to false.
Because the test of the while expression takes place before each execution of the loop, a while loop executes zero or more times. This differs from the do loop, which executes one or more times.
A while loop can be terminated when a break, goto, return, or throwstatement transfers control outside the loop. To pass control to the next iteration without exiting the loop, use the continuestatement. Notice the difference in output in the three previous examples, depending on where int n is incremented. In the example below no output is generated.
For more information, see the following sections in the C# Language Specification:
18.104.22.168 While statements
8.8.1 The while statement