How to use C# while loop
The while statement continually executes a block of statements until a specified expression evaluates to false . The expression is evaluated each time the loop is encountered and the evaluation result is true, the loop body statements are executed.
Like if statement the while statement evaluates the expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.
The C# while statement executes a statement or a block of statements until a specified expression evaluates to false . The above program the loop will execute the code block 4 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.
An empty while-loop with this condition is by definition an infinite loop. You can implement an infinite loop using the while statement as follows: