Last Updated : 12 Dec, 2024
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 Loopdo {
// 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:
Let's understand the working of the do while loop using the given image:
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 LoopLet’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 LoopsJust 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;
}
* * * * * * * * * * * * * * * *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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4