In C++, a do-while loop repeats a block of statements while a condition remains true. Unlike a while loop, the body of a do-while loop runs before the condition is tested, so the body executes at least once. This tutorial covers the syntax, execution flow, flowchart, comparison with a while loop, practical examples, break, continue, infinite loops, and nested do-while loops.
How the C++ Do-While Loop Works
A C++ do-while loop is a post-test loop. The statements inside the do block execute first. After that iteration finishes, the condition in the while clause is evaluated. If the condition is true, the loop repeats. If the condition is false, execution continues with the statement after the loop.
This behavior is useful when an operation must happen once before you can decide whether it should repeat, such as displaying a menu, reading and validating input, or asking a user whether to continue.
C++ Do-While Loop Syntax
Following is the syntax of a do-while loop in C++.
do {
// statement(s)
} while (condition);
The body between do { and } runs first. Then condition is checked. A true condition starts another iteration; a false condition ends the loop.
The semicolon after while (condition) is part of the do-while syntax and must be present:
} while (condition);
Do-While Loop Execution Algorithm
The execution sequence for a C++ do-while loop is:
- Enter the
doblock. - Execute the statement or statements in the loop body.
- Evaluate the condition after the body finishes.
- If the condition is
true, return to the beginning of thedoblock. - If the condition is
false, exit the loop and continue with the next statement.
Variables used by the condition usually need initialization before the loop and an update during each iteration. If the loop condition can never become false, the loop can run indefinitely.
C++ Do-While Loop Flowchart
The following flowchart shows that the loop body is reached before the condition is tested.
C++ While Loop vs Do-While Loop
The main difference between C++ While Loop and C++ Do-While Loop is when the condition is evaluated.
| Behavior | while loop | do-while loop |
|---|---|---|
| Condition check | Before the body | After the body |
| Minimum body executions | Zero | One |
| Best fit | When the condition must be checked before any work is done | When the body must run once before deciding whether to repeat |
For example, if a condition is false before execution begins, a while loop skips its body. A do-while loop still executes its body once and checks the condition afterward.
C++ Do-While Loop Examples
1. Print a String Five Times with Do-While
In this example, the loop prints the string Hello five times. The counter starts at 0 and is incremented as part of the loop condition.
C++ Program
#include <iostream>
using namespace std;
int main() {
int i=0;
do {
cout << "Hello" << endl;
} while (++i < 5);
}
Output
Hello
Hello
Hello
Hello
Hello
After each print, ++i increments the counter before comparing it with 5. When i becomes 5, the condition is false and the loop stops.
2. Compute a Factorial with a Do-While Loop
In this example, a do-while loop multiplies the integers from 1 through 5. The result is 5! = 120.
C++ Program
#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
This form assumes the loop body should run at least once. For a general factorial program that accepts arbitrary input, handle special cases such as 0! explicitly before using a loop.
3. Demonstrate the At-Least-Once Behavior
The following program starts with a condition that is already false. Even so, the do-while body executes once because the condition is checked only after the first iteration.
#include <iostream>
using namespace std;
int main() {
int number = 10;
do {
cout << "Executed once" << endl;
} while (number < 5);
return 0;
}
Output
Executed once
Exit a C++ Do-While Loop with break
You can end a do-while loop before its condition becomes false by using the break statement. When break executes, control leaves the nearest enclosing loop immediately.
In the following example, the loop is capable of counting from 1 to 10, but it stops when i becomes 4. Therefore, only 1, 2, and 3 are printed.
C++ Program
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
if (i == 4) {
break;
}
cout << i << "\
";
} while (++i <= 10);
}
Output
1
2
3
Skip a Do-While Iteration with continue
The continue statement skips the remaining statements in the current iteration. In a do-while loop, control then proceeds to the loop’s condition check. If the condition is true, the next iteration begins.
In the following example, when i is 4, continue skips the cout statement. The condition contains ++i, so the counter still advances before the loop repeats.
C++ Program
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
if (i == 4) {
continue;
}
cout << i << "\
";
} while (++i <= 10);
}
Output
1
2
3
5
6
7
8
9
10
The value 4 is not printed. When using continue, make sure the loop-control variable still changes. Otherwise, the condition may remain true forever.
Infinite C++ Do-While Loops
A do-while loop becomes infinite when its condition never evaluates to false and no statement such as break exits the loop.
Conditions such as true can intentionally create an indefinite loop. In ordinary counter-controlled loops, however, an infinite loop usually indicates that the condition or update logic is incorrect.
In the following example, the condition i==i is always true, so the do-while loop keeps running until the program is interrupted.
C++ Program
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << "hello" << "\
";
i++;
} while (i==i);
}
Output
The string “hello” is printed to the terminal indefinitely, until you interrupt and stop the program execution.
Nested C++ Do-While Loops
A do-while loop can appear inside another do-while loop. The inner loop completes its iterations for each iteration of the outer loop.
In the following program, the outer do-while loop controls the rows of a triangle, while the inner do-while loop prints the required number of * characters in each row.
C++ Program
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
int j = 1;
do {
cout << " *";
} while (++j <= i);
cout << "\
";
} while (++i <= 5);
}
Output
*
* *
* * *
* * * *
* * * * *
The outer loop determines how many rows are produced. For each row, j is reset to 1, and the inner loop prints one more star as the row number increases.
When to Use a C++ Do-While Loop
Choose a do-while loop when the operation must occur before its continuation condition can be evaluated. Common examples include repeating a menu after showing it once, accepting input until it passes validation, and processing an action before asking whether to repeat it.
If the condition should be checked before the first execution, use a while loop instead. If the number of repetitions is known or naturally counter-based, a for loop is often clearer.
Common C++ Do-While Loop Mistakes
- Forgetting the trailing semicolon: the syntax ends with
while (condition);. - Not updating the loop-control value: this can leave the condition true forever.
- Assuming the body can execute zero times: a do-while loop always executes its body once before the first condition check.
- Using continue without considering the update: ensure the variable controlling the condition still changes when an iteration is skipped.
- Choosing do-while when pre-checking is required: use
whilewhen the body must not run unless the condition is already true.
C++ Do-While Loop Summary
A C++ do-while loop is a post-test loop whose body executes at least once. The condition is evaluated after each iteration, and the loop repeats while that condition is true. You can use break to exit early, continue to skip the rest of an iteration, and nested do-while loops for repeated two-level processing.
In this C++ Tutorial, we learned the syntax and execution flow of the do-while loop, compared it with a while loop, and used example C++ programs to demonstrate common do-while patterns.
TutorialKart.com