Hit enter after type your search item

C++ Do While Loop: Explained with 5+ Programs

What is the purpose of do while loop?

In the previous tutorial, we learned to use the for loop in C++ if we know how many times to execute a block of code.

The C++ while loop is used to execute a block of code until a certain condition is true.

Do while loop (the topic of this tutorial), is just a variant of the while loop. It also keeps on executing a block of code until a certain condition is true.

However, the difference is, the do-while loop executes the block of code at least once even if the condition is false at the first iteration.

In the case of while loop, if the condition is false at the first iteration – the while loop terminates.

So, the do-while is useful where you at least required to execute the loop body once.

Structure of do while in C++

Following is the general way of using the do while loop in C++:

Whereas this is how while loop is used to see the immediate difference between the two:

You can see, the placement of condition is the difference – that makes sense why do while loop executes the loop-body at least once.

Now let us see a few examples of using do while loop.

A simple C++ program using do while loop

  • This C++ program simply displays the numbers from 1 to 10.
  • For that, we have a variable x with initial value = 1
  • Then we used the do while loop and inside the loop body we displayed the current value of x.
  • The next line increments the value of x by 1.
  • Outside the brackets, the while statement is used where the condition is tested.



What if the condition is false upfront in do while?

Now let us see the difference between a while loop and do while loop. We used the same code as in the above example except for the initial value of variable x = 15.

See the do while in action:




You saw, the statement inside the do-while body executed and it displayed the current value of x, even the condition is false.

Now see what happens if used the same in the simple while loop:



As the condition was false at the first iteration, the loop body did not execute even once – so the program displayed nothing.

An example of do while loop with a decrement of 2

In this program, we used the do-while loop with the decrement operator. In each iteration, the value of variable x is decremented by 2. So, we will display even numbers between 0 to 20 by using do while.

Have a look:



The example of nested do while loop

Using a do-while loop inside another do-while is called a nested do while loop and you may use it in C++.

The following program shows how you may use it:



For each iteration of the outer loop, the inner loop runs three times.

An example of using break statement in do while loop

As such, the break statement is used to quit a loop in between. We have seen examples in for loop and while loop.

In the following program, we will use the break statement to exit the do while loop. The program displays the value of variable x.

It should display from 5 to 100 with a gap of 5 in each iteration.

However, as the value of x reaches 55, we used the break statement to quit the do-while loop:



You saw, the do-while loop terminated as the value of x reached 55.

Using continue statement example

For omitting one or more iterations rather than entirely quitting the do-while or other loop types, we use the continue statement.

The following example shows an example of its usage:



In the output, you can see two values are missed from 5 to 50 i.e. 15 and 35.

This div height required for enabling the sticky sidebar