C/C++ do while loop with Examples - GeeksforGeeks

22 Jun, 2021
Loops in C/C++ come into use when we need to repeatedly execute a block of statements. Like while the do-while loop execution is also terminated on the basis of a test condition. The main difference between a do-while loop and while loop is in the do-while loop the condition is tested at the end of the loop body, i.e do-while loop is exit controlled whereas the other two loops are entry controlled loops.Note: In do-while loop the loop body will execute at least once irrespective of test condition.  
Syntax:  
do
{
   // loop body

   update_expression;
} 
while (test_expression);
Note: Notice the semi – colon(“;”) in the end of loop.The various parts of the do-while loop are:  
Test Expression: In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update expression. Otherwise, we will exit from the while loop. Example:  
Test Expression: In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update expression. Otherwise, we will exit from the while loop. Example:  
i <= 10
 Update Expression: After executing the loop body, this expression increments/decrements the loop variable by some value. Example:  
 
Update Expression: After executing the loop body, this expression increments/decrements the loop variable by some value. Example:  
i++;
 
 
How does a do-While loop executes?  
Control falls into the do-while loop.The statements inside the body of the loop get executed.Updation takes place.The flow jumps to ConditionCondition is tested. If Condition yields true, goto Step 6.If Condition yields false, the flow goes outside the loopFlow goes back to Step 2.
Control falls into the do-while loop.
The statements inside the body of the loop get executed.
Updation takes place.
The flow jumps to Condition
Condition is tested. If Condition yields true, goto Step 6.If Condition yields false, the flow goes outside the loop
If Condition yields true, goto Step 6.If Condition yields false, the flow goes outside the loop
If Condition yields true, goto Step 6.
If Condition yields false, the flow goes outside the loop
Flow goes back to Step 2.
Flow Diagram do-while loop:  
Example 1: This program will try to print “Hello World” depending on few conditions.  
C
C++
// C program to illustrate do-while loop #include <stdio.h> int main(){    // Initialization expression    int i = 2;     do {        // loop body        printf("Hello World\n");         // Update expression        i++;    }    // Test expression    while (i < 1);     return 0;}
// C++ program to illustrate do-while loop #include <iostream>using namespace std; int main(){    // Initialization expression    int i = 2;     do {        // Loop body        cout << "Hello World\n";         // Update expression        i++;     }    // Test expression    while (i < 1);     return 0;}
Hello World
 
Dry-Running Example 1:  
1. Program starts.
2. i is initialised to 2.
3. Execution enters the loop
  3.a) "Hello World" gets printed 1st time.
  3.b) Updation is done. Now i = 2.
4. Condition is checked. 2 < 2 yields false.
5. The flow goes outside the loop.
Example 2:  
C
C++
// C program to illustrate do-while loop #include <stdio.h> int main(){    // Initialization expression    int i = 1;     do {        // Loop body        printf("%d\n", i);         // Update expression        i++;     }    // Test expression    while (i <= 5);     return 0;}
// C++ program to illustrate do-while loop #include <iostream>using namespace std; int main(){    // Initialization expression    int i = 1;     do {        // Loop body        cout << i << endl;         // Update expression        i++;     }    // Test expression    while (i <= 5);     return 0;}
1
2
3
4
5
 
Related Articles:  
Loops in C and C++C/C++ while loop with ExamplesC/C++ For loop with ExamplesDifference between while and do-while loop in C, C++, JavaDifference between for and while loop in C, C++, Java
Loops in C and C++
C/C++ while loop with Examples
C/C++ For loop with Examples
Difference between while and do-while loop in C, C++, Java
Difference between for and while loop in C, C++, Java
 
anikaseth98
C Language
C++
School Programming
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Multidimensional Arrays in C / C++
rand() and srand() in C/C++
Left Shift and Right Shift Operators in C/C++
fork() in C
Core Dump (Segmentation fault) in C/C++
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
Inheritance in C++
C++ Classes and Objects