The do statement executes a statement or a block of statements repeatedly until a specified expression evaluates to false. It takes the following form:
do statement while (expression);
where:
Unlike the while statement, the body loop of the do statement is executed at least once regardless of the value of the expression.
using System; public class TestDoWhile { public static void Main () { int x; int y = 0; do { x = y++; Console.WriteLine(x);} while(y < 5); } }
0 1 2 3 4
Notice in this example that although the condition evaluates to false, the loop will be executed once.
using System; class DoTest { public static void Main() { int n = 10; do { Console.WriteLine("Current value of n is {0}", n); n++; } while (n < 6); } }
Current value of n is 10
C# Keywords | Compare to C++ | Iteration Statements | Grammar