C++ do while Loop

The do while loop in C++ is similar to while loop with one important difference, the code block of do..while loop is executed once before the condition is checked. It checks its condition at the bottom of the loop, before allowing the execution of loop body for second time. The block of code is before the condition, so code will be executed at lease once.

Syntax of do while Loop

do {
    statement(s);// statements to be executed
}while(condition);
do while Loop Syntax Description
  • condition : The condition expression in do while loop is a boolean expression. Condition expression is evaluated after code block of do-while loop. It must evaluate to true or false value(In C++, all non-zero values are considered as true and zero is considered as false).
  • statement(s) : do while loop's body can contain zero or more number of statements. Unlike for and while loop, all statements inside do-while loop will gets executed atleast once.
  • Unlike while loop, there is semicolon in the end of while(condition); in do..while loop.

Flow diagram of do while Loop in C++

C++ Do While Loop Statement Control Flow Diagram
How do while loop works ?
  • First do while loop execute the statements inside body of the loop.
  • Then, the condition expression is tested. If the condition expression evaluates to true then body of do..while loop is executed.
  • If the condition expression evaluates to false then do..while loop is terminated and control goes to the next statement after do..while loop.
NOTE : Do While loop can get terminated from loop body by executing break statement.

C++ do..while Loop Example Program

/* 
 * C++ program to find the sum 
 * of all integers from 1 to N using do while loop. 
 */
#include <iostream>
using namespace std;
 
int main(){

    int N, i, sum=0;
    cout << "Enter a positive number\n";
    cin >> N;
    /* 
     * Initializing loop control variable before do while loop 
     */
    i = 1;
    do {
     // Below statements will execute atleast once
        sum+= i;
        //Incrementing loop control variable 
        i++;
    } while(i <= N);
    cout <<"Sum of Integers from 1 to " << N << " = " << sum;
     
    return 0;
}

Output
Enter a positive number
5
Sum of Integers from 1 to 5 = 15
Enter a positive number
7
Sum of Integers from 1 to 7 = 28

Points to Remember about do while loop
  • do…while loop is guaranteed to execute at least once.

  • The condition expression in do while loop is a boolean expression. It must evaluate to true or false value(In C++, all non-zero values are considered as true and zero is considered as false).

  • Unlike for loop, initialization statements, condition expression and update statements are on different line.

  • Opening and Closing braces are not required for single statement inside do while loop code block.
    For Example:
        do
          statement;
        while(condition);

  • We can also use infinite do while loops which will never terminate. You should add terminating condition using break statement in order to terminate infinite do while loop.
    For Example:
      do {
         if(....){
           break;
         }
     } while(1);