C# Language Keywords do

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

The do operator iterates over a block of code until a conditional query equals false. The do-while loop can also be interrupted by a goto, return, break or throw statement.

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 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);


Got any C# Language Question?