The while statement executes a statement or a block of statements until a specified expression evaluates to false. It takes the form:
while (expression) statement
where:
Because the test of expression takes place before each execution of the loop, a while loop executes zero or more times.
A while loop can be terminated when a break, goto, return, or throw statement transfers control outside the loop. To pass control to the next iteration without exiting the loop, use the continue statement.
using System; class WhileTest { public static void Main() { int n = 1; while (n < 6) { Console.WriteLine("Current value of n is {0}", n); n++; } } }
Current value of n is 1 Current value of n is 2 Current value of n is 3 Current value of n is 4 Current value of n is 5
C# Keywords | Compare to C++ | Iteration Statements | Grammar