C++ Do While Loop

What is the purpose of do while loop?

In the previous tutorial, we learned how 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.

The difference is that 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 a while loop, if the condition is false at the first iteration – the while loop terminates.

So, the do-while is useful where you are 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++:

do {

   statement(s) to execute here;

}

while( condition );

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

while(condition) {

   statement(s) to execute here;

}

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.
#include <iostream>

using namespace std;

int main() {

     int x = 1;

          do{

              cout<<"value of x = " <<x<<"\n";

              x++;

          }

          while (x <= 10) ;

}

Output:

CPP-do-while-loop

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:

#include <iostream>
using namespace std;
int main() {
int x = 15;
do{
cout<<"value of x = " <<x<<"\n";
x++;
}
while (x <= 10) ;
}

Output:

value of x = 15

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:

#include <iostream>

using namespace std;

int main() {

    int x = 15;

    while (x <= 10) {

        cout <<"Current value of x = " << x << "\n";

        x++;

    }

    return 0;

}

Output:

CPP-do-while-diff-2

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:

#include <iostream>

using namespace std;

int main() {

     int x = 20;

          do{

              cout<<"x = " <<x<<"\n";

              x=x-2;

          }

          while (x >= 0) ;

}

Output:

x = 20
x = 18
x = 16
x = 14
x = 12
x = 10
x = 8
x = 6
x = 4
x = 2
x = 0

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:

#include <iostream>

using namespace std;

int main() {

   int x = 1;

    do {

     int y = 1;

       do {
        cout <<x <<" " << y << "\n";
        y++;

   } while (y <= 3);

       x++;

  } while (x <= 3);

}

Output:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

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:

#include <iostream>

using namespace std;

int main() {

     int x = 5;

          do{

            cout<<"x = " <<x<<"\n";

            x=x+5;

            if (x == 55) {

            break;

  }

          }

          while (x <= 100) ;

}

Output:

x = 5
x = 10
x = 15
x = 20
x = 25
x = 30
x = 35
x = 40
x = 45
x = 50

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

Using the 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:

#include <iostream>

using namespace std;

int main() {

     int x = 5;

          do{

          //if statement to check x value and executing continue statement

          //Placement is important

          if (x == 15 || x ==35) {

            x=x+5;

            continue;

  }

            cout<<"x = " <<x<<"\n";

            x=x+5;

          }

          while (x <= 50) ;




}

Output:

x = 5
x = 10
x = 20
x = 25
x = 30
x = 40
x = 45
x = 50

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

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!