C# do-while Loop

C# do-while loop works in the same way as the while loop, except that it always performs at least one iteration at the start even if the condition is false. The stopping condition is checked at the bottom of the loop when the while statement is encountered. Keep in mind that this loop ends with a semicolon (;).

Within a do-while loop, you can use the continue and break statements to control the flow of execution in the loop.

Do While Loop Syntax

C# Do While Loop

To form a do-while loop, you put the do keyword at the start of the loop and then the loop body. At the end, you put the while keyword followed by a condition within parentheses (). The condition is an expression that returns a boolean value of true or false. Any other data type is not acceptable as a condition.

  • The do-while loop is a posttest loop because it does not test its condition until it has completed an iteration. 
  • Loop takes only one condition, which is checked at the bottom of the loop instead of the top of the loop.
  • Loop always executes the code block at least once in the beginning.
  • The condition is evaluated after each iteration, which must be true to perform the next iteration. The iterations end when the condition at the end becomes false.
  • The body can be a single statement or a block of statements surrounded by curly braces.
  • If the loop’s condition is constantly true, the loop will never end. 
  • A statement inside the loop must eventually make the loop’s condition false during an iteration to avoid an infinite or endless loop.
  • Loop must end with a semicolon; leaving it out is a common error.

C# Do While Loop Flowchart

C# Do While Loop Flowchart

The flowchart of a do-while loop is simple: Initially, the loop body is executed at least once, and then the condition is tested, which evaluates to true or false. If the condition is true, the execution flows back to the top just above the first statement in the loop’s body and executes. The condition is checked again. If the condition is still true, the whole process starts over again. This logic repeats until the condition at the end becomes false. In that case, C# exits the do-while loop and the next first statement after the loop continues.

C# Do While Loop Example

When coding the do-while loop, it’s common to use a counter variable to execute the statements in a loop a certain number of times. Here, the counter variable is an int type name i, and is assigned an initial value of 0 just before C# executes the do statement. The below code shows an example of a C# do-while loop:

int i = 0;

do
{
    i++;

    Console.WriteLine(i);

}
while (i < 4);

Test Code

The first statement (i++) in the loop’s body increments the counter with each repetition of the loop, thus increasing the counter variable value i by 1 using an increment operator (++). 

As a result, the other statement Console.WriteLine(i); in the loop body will repeatedly execute until the counter variable is less than 4. Eventually, i will become equal to 4, so the Boolean condition i < 4 will fail and the loop will end. The do-while loop finishes by displaying all the numbers from 1 to 4 on the console.

Notice that a semicolon (;) appears at the very end of the while statement; leaving it out is an error.

OUTPUT

1
2
3
4

Let’s see another example where do-while loop executes one time because the loop does not test the expression i < 0 until the end of the iteration.

int i = 1;

do
{  
  Console.WriteLine(i);
} while (i < 0);

Test Code

OUTPUT

1

This is because do-while loop is a posttest loop. That means it does not test its condition until it has completed an iteration. As a result, the loop always executes at least one iteration, even if the condition is false in the beginning.

C# Do While Loop Break

C# Do While Loop Break

Jumping out of a loop with a break operator. C# break statement causes the do-while loop to end processing prematurely before it has completed the execution and passes control to the first statement immediately after the loop. A loop’s termination with a break can only be done from its body during an iteration of the loop.

C# Do While Loop Break

Test Code

When the counter reaches 4, the condition (i == 4) evaluates to true. The break keyword then skips the code in the loop’s body and resumes execution immediately following the loop. Loop skipped the value 4.

OUTPUT

1
2
3

C# Do While Loop Continue

C# Do While Loop Continue

Skipping to the next iteration with a continue operator. When you use C# continue statement, the loop skips to the closing brace of the do block for the current iteration and then continues with the next iteration. The continue statement passes control straight to the while statement to start over.

C# Do While Loop Continue

Test Code

In the above code example, the loop prints out all the numbers except one. It skips 3 due to the continue statement. When the loop hits condition (i == 3), it immediately skips Console.WriteLine(i) and goes directly to the while statement to check the condition. If the condition is still true, the loop than continues on with the next cycle through the loop.

OUTPUT

1
2
4

Infinite Do While Loop in C#

When you code do-while loop, it’s possible to accidentally code an infinite or endless loop, which is a loop that may never stop. Infinite loops usually occur when the programmer forgets to write code inside the loop that makes the test condition false.

This type of loop is rarely needed except in cases where somewhere in the loop’s body a break statement is used to end its execution prematurely.

For example, if i has a value of 4.

int i = 4;

do
{
    i -= 2;
}
while (i < 12);

The value of i is 2, 0, -2, -4, -6,. . ., and so on. The condition, i < 12, is always true, so the loop keeps on executing until you abort the program.

int i = 0;

do
{
    //i++; forgot to add this

    Console.WriteLine(i);

}
while (i < 4);

Another perfectly valid example is:

do
{
    Console.WriteLine("Infinite While Loop.");
} while (true);

C# Do While vs While

Already discussed here.

Community Input

💬 What other C# do-while loop topics should I cover here?

Please use twitter or instagram to share your suggestions. We will add your feedback here.

Share With Love ❤️

Like this on Instagram

Like this on twitter

Resources

  1. do-while loop
  2. while loop
Sending
User Rating 5 (1 vote)

TidBits

Most Viewed TidBits