do

suggest change

The do statement runs a block of code, then repeats it until the condition is false. The loop can also be left by goto, return, break, or throw.

The syntax for the do keyword is:

do { code block; } while( condition );

Example:

int i = 0;

do
{
    Console.WriteLine("Do is on loop number {0}.", i);
} while (i++ < 5);

Output:

Do is on loop number 0. Do is on loop number 1. Do is on loop number 2. Do is on loop number 3. Do is on loop number 4. Do is on loop number 5.

Unlike the while loop, the do-while loop is Exit Controlled. This means that the do-while loop would execute its statements at least once, even if the condition fails the first time.

bool a = false;

do
{
    Console.WriteLine("This will be printed once, even if a is false.");
} while (a == true);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents