C++ do while loop, syntax and use

As like as for loop and while loop, do while loop is also used to iterate a block of code several times. But there is a difference with other loop of do while loop. The do while loop will iterate at last once although the condition becomes false. This is because it checks the condition after executing the code.

We use do while loop where we need to execute the code at last one time and it is recommended to use do while loop where the number of iteration is not fixed.

The syntax of do while loop is as follows;

// syntax of do while loop in C++

variable initialization;

do{
    // block of code to execute
    
    increment or decrement;
    
} while {condition};

We have to initialize the initial value of the variable in variable initialization section.

Then inside the do keyword we have to write the code which will executed at last once as we have not check any condition yet. Then the increment or decrement of variable.

We have to write the condition inside the while keyword. Now, let’s see a C++ program using do while loop.

C++ program using do while loop.

// c++ program using do while loop

#include <iostream>
using namespace std;

int main(){

    int k = 1;    // initialize variable k

    do{
        cout << k << endl;

        k = k + 2;    // incrementing value of k

    }while(k < 10);    // condition to check

    return 0;
}

Output of do while loop program

do-while-loop-in-c-c-do-while-loop

How the program work?

Let’s try to understand through the given table that how this do while loop program works.

Value of k Action Incrementing k k < 10
1
print k = 1
k = 3
true
3
print k = 3
5
true
5
print k = 5
7
true
7
print k = 7
9
true
9
print k = 9
11
false & loop break

And for this, the program will print 1,  3,  5,  7 and 9. We will see more example program in our C++ programming section.

We can also write a do while loop inside another do while as like as for loop and while loop in C++. This is called nested do while loop. Let’s see following C++ program using nested do while loop.

Program using nested do while loop

In case of nested loop the inner loop will iterate full times for each iteration of the outer loop.

// c++ program using nested do while loop

#include<iostream>
using namespace std;

int main(){
    int x = 1;

    do{    // outer loop
        cout << x << ". Outer do while loop" << endl;
        int y = 1;

        do{    // inner loop
            cout << "\t" << y << ". Inner do while loop" << endl;
            y++;    // incrementing for inner loop
        } while (y < 4) ;    // condition for inner loop

        x++;    // incrementing for outer loop

    } while(x < 5) ;    // condition for outer loop

    return 0;
}

Program output:

Sometimes the condition of a loop becomes never false. This type of loop is called infinite loop. We already have discussed infinite for loop and infinite while loop. Here, we will see infinite do while loop.

What we have to do for creating an infinite loop is using a condition that will always be true. Let’s see the example program of infinite do while loop.

Infinite do while loop in C++

// infinite do while loop in c++

#include <iostream>
using namespace std;

int main(){
    do{
        cout<<"Never stop because infinite loop\n";

    } while(true);    // always be true
    
    return 0;
}

As the condition is always be true and for this the program will never stop. So, this is an infinite do while loop.

You can create an infinite do while loop using any condition which will always become true. Some infinite condition may be looks like as following.

while(!false);

while(true);

while(3 > 2);

while(5 < 9);

while(2 != 4);

// As well as some other true expression

Previous pageWhile loop in C++