C# Basics

C# do-while loop

Table of Contents

The 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.

The outline of this tutorial is as follows:

  • You’ll start with the C# do-while loop syntax and execution order used for indefinite iteration (executes until some condition is met)
  • Then breaking out of a loop or loop iteration prematurely
  • Skip and start next iteration without breaking out of the loop.
  • Finally, you will explore infinite loop.

Ready? Here we go!

Edit, and test all the CODE examples used in this article with C# online editor:  Code Playground

Syntax

C# do-while Loop Syntax

To form a do-while loop, you put the “do” keyword at the start of the loop and then the loop body. In 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 continuously 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.

Flowchart

C#'s 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);

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

OUTPUT

1

This is because the 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.

Curly brackets for the loop are optional if there is only one statement in the code block.

int counter = 0;

do
  counter++;
while (counter < 4);

The break Keyword With do-while Loop

The break Keyword With do-while Loop

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.

The break Keyword With do-while Loop Code Example

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


The continue Keyword With do-while Loop

The continue Keyword With do-while Loop

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.

The continue Keyword With do-while Loop Code Example

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 then continues with the next cycle through the loop.

OUTPUT

1
2
4


Infinite do-while Loop

When you code a 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 when 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);

Difference Between do-while Loop and while Loop

Already discussed here.