In this C++ tutorial, you will learn the syntax of Do-While loop statement, its algorithm, flowchart, then some examples illustrating the usage of it. Later in the tutorial we shall go through Infinite Do-While Loop and Nested Do-While Loop.

C++ Do-While Loop

Do-While Loop can execute a block of statements in a loop based on a condition.

Syntax of Do-While

Following is the syntax of while loop in C++.

</>
Copy
do {
  // statement(s)
} while (condition);

statement(s) inside do block are executed and the while condition is checked. If the condition is true, statement(s) inside do block are executed. The condition is checked again. If it evaluates to true, the statement(s) inside the while loop are executed. This cycle goes on. If at all, the condition evaluates to false, program control comes out of the loop, meaning do-while loop execution is completed. And the program continues with the execution of statements after do-while loop if any.

Algorithm

Following would be the algorithm of while loop.

  1. Start.
  2. Execute statement(s).
  3. Check the condition. If the condition is true, go to step 2.
  4. Stop.

You have to take care of the initialization and update of the variables present in condition. And make sure that the condition would end the loop after a definite number of iterations. If the condition is never going to be false, then the do-while loop is going to execute indefinitely.

Flow Diagram

Following is the flow chart of flow diagram of do-while loop in C++.

C++ Do-While Loop

Difference between While and Do-While Loop

The main difference between C++ While Loop and C++ Do-While Loop is that:

In while loop the condition is evaluated first, and based on its value, the block is either executed or not. Whereas in do-while loop, the block of statement(s) is executed first, and then the condition is evaluated, and based on its value, the block is either executed further or not.

Examples

1. Simple Do-While loop statement

In this example, we shall write a do-while loop that prints the string Hello five times.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int i=0;
   do {
      cout << "Hello" << endl;
   } while (++i < 5);  
}

Output

Hello
Hello
Hello
Hello
Hello

2. While loop to compute Factorial

In this example, we shall use do-while loop to compute factorial of a number.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int n = 5;
   int factorial = 1;

   int i = 1;
   do {
      factorial *= i;
   } while (++i <= n);

   cout << factorial << endl;
}

Output

120

Do-While Loop with break statement

You can break the do-while loop before even the condition becomes false using break statement. break statement ends the execution of the wrapping do-while loop.

In the following example, we shall write a do-while loop that prints numbers from 1 to 10. But, then we include a break statement such that when i is 4, we break the loop.

C++ Program

</>
Copy
#include <iostream>  
using namespace std;

int main() {
   int i = 1;
   do {
      if (i == 4) {
         break;
      }
      cout << i << "\n";
   } while (++i <= 10);
}

Output

1
2
3

While Loop with continue statement

You can skip the execution of statements in a while loop during an iteration using continue statement. continue statement takes the control to the condition without executing further statements in the loop.

In the following example, we shall write a while loop that prints numbers from 1 to 10. But, then we include a continue statement such that when i is 4, we skip the execution of further statements inside while loop.

C++ Program

</>
Copy
#include <iostream>  
using namespace std;

int main() {
   int i = 1;
   do {
      if (i == 4) {
         continue;
      }
      cout << i << "\n";
   } while (++i <= 10);
}

Output

1
2
3
5
6
7
8
9
10

Note that the loop execution has been skipped when i has been 4.

Infinite Do-While loop

If the condition in while loop is going to be always true, then this loop runs indefinitely. This kind of loop is called infinite loop.

Just a simple condition like 1==1 or true, will make the while loop to run indefinitely.

In the following example, we start with i=1 and increment it at each iteration. But the condition we check is i==i. This is going to be always true, and hence the do-while loop runs indefinitely.

C++ Program

</>
Copy
#include <iostream>  
using namespace std;

int main() {
   int i = 1;
   do {
      cout << "hello" << "\n";
      i++;
   } while (i==i);
}

Output

The string “hello” is printed to the terminal indefinitely, until you interrupt and stop the program execution.

Nested Do-While loop

Do-While is just like another statement in C++. So, you can include a do-while loop inside the body a do-while loop, just like a statement.

In the following example program, we shall print a pattern that resembles a triangle, using nested do-while loop.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int i = 1;
   do {
      int j = 1;
      do {
         cout << " *";
      } while (++j <= i);
      cout << "\n";
   } while (++i <= 5);
}

Output

 *
 * *
 * * *
 * * * *
 * * * * *

Outer do-while loop is used to traverse the rows, and inner do-while loop is used to traverse the columns.

Conclusion

In this C++ Tutorial, we learned the syntax of do-while loop in C++, its algorithm, flowchart, and usage with the help of example C++ programs.