Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All


The while statement executes a statement or a block of statements until a specified expression evaluates to false. It takes the following form:

while (expression) statement


An expression that can be implicitly converted to bool or a type that contains overloading of the true and false operators. The expression is used to test the loop-termination criteria.
The embedded statement(s) to be executed.


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.


// statements_while.cs
using System;
class WhileTest 
   public static void Main() 
      int n = 1;

      while (n < 6) 
         Console.WriteLine("Current value of n is {0}", 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

See Also

C# Keywords | Compare to C++ | Iteration Statements | C. Grammar

© 2015 Microsoft