C++ do while Loop Structure (with example programs)

- November 21, 2017
The do-while loop is also used to execute a statement or set of statements repeatedly as long as the given condition remains true. The 'do-while' loop is mostly used for menu selection or to enter records. In menu selection, we have to execute the body of loop at least once.
In C++, the general syntax of the do-while loop is as follows:
  do
  {
     body of loop;
  }
     while (condition);
Where:
do It is a keyword of C++ that indicates the beginning of the 'do-while' loop.
body of loop It is a statement or set of statements that will be executed repeatedly.
while (condition) It is the last statement of 'while-do' loop structure. This statement contains the test condition.

  There is a semicolon ";" at the end of keyword "while". The semicolon is necessary at the end of this statement.

Flowchart of 'do-while' Loop

The flowchart of the 'do-while' loop structure is given below.
do while loop in C++

Working of do-while Loop

When the 'do-while' statement is executed, first the body of loop is executed and then the condition is evaluated. If the given condition (case) is true then execution control goes back to the beginning of the 'do-while' loop. This process is repeated again and again until the given condition remain true. When the given condition becomes false at any stage during program execution, the loop is terminated (ended). The control shifts to the statement that comes immediately after the 'do-while' loop structure.
In the previous tutorial we discussed about C++ 'while' loop structure. To be careful from complexity we should know difference between 'while' loop and 'do-while' loop structure. So read main difference between the 'while' loop structure and 'do-while' loop structure that is given below:

Difference between while and do-while loop

  • In 'while' loop the semicolon (;) is not given after the while (condition). But in 'do-while' loop the semicolon (;) is necessary at the end of while(condition).
  • In 'while' loop the test condition comes before the body of loop. But, in 'do-while' loop the test condition comes after the body of the loop.
  • In 'while' loop structure, the beginning of the loop, first condition is evaluated and then body of loop is executed if the condition is true.  But in 'do-while' loop structure, the condition is evaluated after executing the body of loop. The body of the loop must be executed at least once even if the condition is false.

Example Programs

To clear the concept about C++ 'do-while' loop structure, there are a few examples of the programs that are written using 'do-while' loop. Look at these sample program to understand how the 'do-while' loop works and how to use this loop.

Example 1: The following program displays the following series using 'do-while' loop.
100            10
90             40
80             70
70            100

#include <iostream>
using namespace std;
main ()
{
  int m, n;
m= 100;
n= 10;
do
{
   cout<<m<<"\t\t"<<n<<endl;
   m-= 30;
   n+= 30;
} while (n<=100);
return 0;
}
Example 2: The following program displays five numbers from 1 to 5 and their cube using the do-while loop statements.

#include <iostream>
using namespace std;
main ()
{
int n;
n= 1;
cout<<"Number\t Cube \n";
do
{
cout<<n<<'\t'<<n*n*n<<endl;
n= n+1;
} while (n<=5);
return 0;
}
Output:
Number      Cube
1      1
2      8
3      27
4      64
5      125

Example 3: The following program computes the sum of the following series using the 'do-while' loop.
1+1/3+1/5+.............+1/99
#include <iostream>
using namespace std;
main ()
{ 
  float s, n;
  s= 0.0;
  n= 1.0;
do
{
  s = s+1/n;
  n = n+2;
} while (n<=99);
cout<<"Sum of the series ="<<s;
return 0;
}
Output:
Sum of the series = 2.93775

Example 4: The following program inputs two integers from user and calculates first number raised to the power of second number using the do-while loop statement.

#include <iostream>
using namespace std;
main ()
{
  int n, p, res, c;
  cout<<"Enter first number:";
  cin>>n;
  cout<<"Enter second number:";
  cin>>p;
  res= c = 1;
do
{
     res = res * n;
     c = c+1;
} while (c<=p);
cout<<res;
return 0;
}
Output:
Enter the first number: 2
Enter the second number: 4
16

Example 5: The following program displays the following series using the do-while loop.
100          10
90            40
80            70
70            100
#include <iostream>
using namespace std;
main ()
{
  int m, n;
  m= 100;
  n= 10;
do
{
  cout<<m<<"\t\t"<<n<<endl;
  m-=30;
  n+=30;
} while (n<=100);
return 0;
}
Example 6: The following program inputs an integer and displays it in reverse order. Such as, if the user enters 12345, it should be displayed as 54321.
#include <iostream>
using namespace std;
main ()
{
  long int n, m, r, rev=0;
cout<<"Enter an integer value: ";
cin>>n;
m= n;
do
{
r= n%10;
rev = (rev*10) +r;
n=n/10;
} while (n!=0);
cout<<m<<" in reverse order is "<<rev<<endl;
return 0;
}
Example 7: The following program displays the multiplication table of a given number using the 'do-while' loop.
#include <iostream.h>
using namespace std;
main ()
{
  int n, tab;
cout<<"Enter a number:";
cin>>tab;
n = 1;
do
{
cout<<n<<" x" <<tab<<" = "<<tab*n<<endl;
n++;
} while (n<=10);
return 0;
}
Example 8: The following program inputs starting and ending number and displays all odd numbers in the given range using the 'do-while' loop.
For example, if the starting number is 10 and the ending number is 15, the odd numbers of this range are 11, 13, and 15.
#include <iostream>
using namespace std;
main ()
{
  int num, start, end;
  cout<<"Enter the starting number:";
  cin>>start;
  cout<<"Enter the ending number:";
  cin>>end;
  num = start;
do
{
  if (num%2==1)
     cout<<num<<endl;
   num++;
} while (num <=end);
return 0;
}
Output:
Enter starting number: 3
Enter ending number: 10
3
5
7
9

Example 9: The following program displays number from 10 to 1 back counting using the 'do-while' loop statement.
#include <iostream>
using namespace std;
main ()
{
  int num;
  num= 10;
do
{
  cout<<num<<endl;
  num= num-1;
} while (num>=1);
cout<<"OK";
return 0;
}
Example 10: The following program calculates the sum of first 10 odd and even numbers using a single 'do-while' loop statement.
 It also displays the odd and even numbers in the following way:
1         2
3         4
5         6
.............
#include <iostream>
using namespace std;
main ()
{
int n, odd, even;
  odd=even=0;
n= 1;
do
{
   if (n%==0)
    {
        even= even+n;
        cout<<n<<endl;
     }
     else
     {
         odd= odd+n;
         cout<<n<<"\t";
     }
     n++;
} while (n<=10);
cout<<endl;
cout<<"Sum of even numbers:"<<even<<endl;
cout<<"Sum of odd numbers:";<<odd<<endl;
return 0;
}