Dremendo Tag Line

do while Loop in C++ Programming

Loops in C++

In this lesson, we will understand what is do while loop and how it is used in C++ programming.

What is do while Loop

A do while loop in C++ programming is almost similar to while loop with an only difference that the test condition is checked at the end of the loop. A do while loop is also known as Exit Controlled loop because the test condition is evaluated, after the execution of the body of the do while loop.

In the do while loop, the body of the loop will execute at least once even after the test condition is false.

Syntax of do while Loop

initialization expression;
do
{
    // body of the loop
    // statements we want to execute
    update_expression;
}
while(test_expression);

Example

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    // initialization expression
    int i = 1;

    // test expression
    do
    {
        cout<<"Hello World\n";

        // update expression
        i=i+1;
    }
    while(i<=5);
    return 0;
}

Output

Hello World
Hello World
Hello World
Hello World
Hello World

We initialize the loop variable before starting the do while loop. After initialization, we will start the loop with a do statement. After that body of the do while loop is executed and the update expression is performed and at last test condition of do while is evaluated. When the test expression evaluates to false the do while loop terminates.

Note: There must be a semicolon ; after the end of while statement as shown in the above example.

Example 1

C++ program to print the numbers from 1 to 10 on the screen using do while loop.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=1;
    do
    {
        cout<<i<<endl;
        i=i+1;
    }
    while(i<=10);
    return 0;
}

Output

1
2
3
4
5
6
7
8
9
10

In the above example, we have run a do while loop from 1 to 10. Each time when the loop runs, we print the value of the variable i on the screen on a separate line using \n. The loop ends when the value of i is more than 10.

Example 2

C++ program to print all the even numbers from 10 to 20 on the screen using do while loop.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=10;
    do
    {
        cout<<i<<endl;
        i=i+2;
    }
    while(i<=20);
    return 0;
}

Output

10
12
14
16
18
20

In the above example, we have run a do while loop from 10 to 20. Each time when the loop runs, we print the value of the variable i on the screen on a separate line using \n and then increment the value of i by 2. The loop ends when the value of i is more than 20.

Example 3

C++ program to print all the numbers from 10 to 1 in reverse order on the screen using do while loop.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=10;
    do
    {
        cout<<i<<endl;
        i=i-1;
    }
    while(i>=1);
    return 0;
}

Output

10
9
8
7
6
5
4
3
2
1

In the above example, we have run a do while loop from 10 to 1 in reverse order. Each time when the loop runs, we print the value of the variable i on the screen on a separate line using \n and then decrement the value of i by 1. The loop ends when the value of i is less than 1.

Example 4

C++ program to input a 4-digit number and find the sum of its digits using do while loop.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int n,r,sd=0;
    cout<<"Enter a 4 digit number ";
    cin>>n;

    if(n>=1000 && n<=9999)
    {
        do
        {
            r=n%10;     // return the last digit of the number as remainder
            sd=sd+r;
            n=n/10;     // removes the last digit of the number
        }
        while(n>0);
        cout<<"Sum of digits = "<<sd;
    }
    else
    {
        cout<<"Not a 4 digit number";
    }
    return 0;
}

Output

Enter a 4 digit number 2461
Sum of digits = 13

In the above example, we have input a 4-digit number. The loop starts with the 4-digit number and runs till the value of n is greater than 0. Each time when the loop runs, we find the last digit of the number using n%10 and add the digit in the variable sd and remove the last digit from the number using n=n/10. The loop ends when the value of n becomes 0 after removing all the digits from n.

Nested do while Loop

C++ programming allows using one do while loop inside another do while loop. This is known as nested while loop.

Syntax of Nested while Loop

initialization expression;
do
{
   // body of the outer loop
   // statements we want to execute

   initialization expression;
   do
   {
       // body of the inner loop
       // statements we want to execute

       update expression;
   }
   while(test expression);

   update_expression;
}
while(test expression);

Example

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=1,j;
    do
    {
        j=1;
        do
        {
            cout<<j;
            j++;        // it means j=j+1
        }
        while(j<=i);

        cout<<"\n";
        i++;            // it means i=i+1
    }
    while(i<=5);
    return 0;
}

Output

1
12
123
1234
12345

In the above example, we have run two loops, one outer loop and another inner loop. The outer loop runs from 1 to 5, and the inner loop runs from 1 to the current value of the outer loop (for example, when the value of i is 1, the inner loop runs from 1 to 1. When the value of i is 2, the inner loop runs from 1 to 2 and so on). The program ends when the outer loop test expression evaluates to false.

Infinite do while Loop

An infinite do while loop is a loop that never ends because its test condition never evaluates to false. It keeps on executing unless and until we terminate the loop using the break statement as shown in the example below.

Syntax of Infinite do while Loop in C++

do
{
    // body of the infinite do while loop
}
while(1);

Example

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=0;

    do
    {
        cout<<"Hello World"<<endl;

        i=i+1;
        if(i==5)
        {
            break;  // terminate the infinite loop when the value of i is 5
        }
    }
    while(1);
    return 0;
}

Output

Hello World
Hello World
Hello World
Hello World
Hello World

video-poster