In C++, the do-while loop is an exit-controlled loop that repeatedly executes a block of code at least once and continues executing as long as a given condition remains true. Unlike the while loop, the do-while loop guarantees that the loop body will execute at least once, regardless of whether the condition is true or false.
Let’s take a look at an example:
C++ #include <iostream> using namespace std; int main() { // do-while loop to print "Hi" 5 times int i = 0; do { cout << "Hi" << endl; i++; } while (i < 5); return 0; }
Explanation: The above do-while loop prints the text "Hi" 5 times. It first executes the body of the loop and then checks the condition. Since the condition i < 5 is true for the first few iterations, the loop continues running. Once i reaches 5, the condition becomes false, and the loop exits.
Syntax of do-while Loop
do {
// Body of the loop
// Update expression
} while (condition);
We have to define the loop variable beforehand and update it manually in the body of the loop. Notice the semicolon (";") in the end of loop. It is compulsory to terminate the do while loop after the semicolon.
The various parts of the do-while loop are:
- Condition: The condition is checked after the loop body executes. If the condition evaluates to true, the loop continues. If false, the loop exits.
- Update Expression: Update the loop variable bringing it closer to termination condition.
- Body: Body: It is a group of statement that will be executed surely for the first time and then till the condition remains true.
Working of do while Loop in C++
Let's understand the working of the do while loop using the given image:
- 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 the Condition yields true, go to Step 6.
- If the Condition yields false, the flow goes outside the loop
- The flow goes back to Step 2.
- The do-while loop has been ended and flow has gone outside the loop.
Flow Diagram of do-while loop
Examples of do while Loop in C++
The below examples demonstrate the use of do while loop in different cases and situations:
Print Numbers Less than 0
C++ #include <iostream> using namespace std; int main() { // do-while loop to print "Hi" 5 times int i = 1; do { cout << i << endl; i++; } while (i < 0); return 0; }
Explanation: As we can see, even though the condition is false from the start, the body is still executed once.
User Input Validation with do-while Loop
Let’s consider an example where a user is prompted to enter a positive number. The program will continue to prompt the user until they enter a valid number.
C++ #include <iostream> using namespace std; int main() { int n; // Do-while loop to ensure user enters a positive number do { cout << "Enter a positive number: "; cin >> n; } while (n <= 0); cout << "Entered number: " << n << endl; return 0; }
Output
Enter a positive number: -1
Enter a positive number: -999
Enter a positive number: 2
Entered number: 2
This is one of the primary applications of do while loop in C++.
Print a Square Pattern using Nested Loops
Just like other loops, we can also nest one do while loop into another do while loop.
C++ #include <iostream> using namespace std; int main() { int i = 0; // Outer loop to print each row do { int j = 0; // Inner loop to print each character // in each row do { cout << "* "; j++; } while (j < 4); cout << endl; i++; }while (i < 4); return 0; }
Output* * * * * * * * * * * * * * * *
Infinite do while Loop
C++ #include <iostream> using namespace std; int main() { // Infinite loop do { cout << "gfg" << endl; }while (true); return 0; }
Output
gfg
gfg
.
.
.
infinite times
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems
My Profile