The do while loop in C++ is a looping statement that repeats a block of code while a condition remains true, but with one important difference from the ordinary while loop: the body executes first and the condition is checked afterward. This means a do while loop always runs at least once, even if the condition is false after that first execution.
This behavior makes the do while loop useful when the program must perform an action before deciding whether another repetition is needed. Common examples include menu-driven programs, asking the user for input at least once, retry-style prompts, and state-based tasks where one initial execution is guaranteed. To use it properly, you should understand the syntax, execution flow, how it differs from while, and what mistakes can create unintended infinite loops.
What Is Do While Loop in C++?
A do while loop is an exit-controlled loop. That means the condition is checked after the loop body runs. Because of this, the body is guaranteed to execute one time before the program decides whether to repeat it.
| Property | Meaning |
|---|---|
| Loop type | Exit-controlled |
| Condition check timing | After the loop body |
| Body can run zero times | No |
| Body runs at least once | Yes |
The defining feature of a
do whileloop is simple: first execute, then decide whether to continue.
Syntax of Do While Loop in C++
The syntax looks slightly different from other loops because the condition appears after the body.
do
{
// loop body
} while (condition);Notice the semicolon after the while (condition) part. That semicolon is part of the syntax and must not be forgotten.
Flow of Execution in Do While Loop
- The loop body executes first.
- After the body finishes, the condition is checked.
- If the condition is true, the body runs again.
- If the condition is false, the loop stops.
This order of execution is what separates do while from while. It is especially helpful when the program should ask for input, display a menu, or perform one guaranteed action before deciding whether another iteration is necessary.
Simple Example of Do While Loop
#include <iostream>
int main()
{
int i = 1;
do
{
std::cout << i << std::endl;
i++;
} while (i <= 5);
return 0;
}This loop prints numbers from 1 to 5. The body runs first, then the condition i <= 5 is checked after each iteration.
Do While Loop Runs at Least Once
This is the most important property of a do while loop. Even if the condition is false from the beginning, the body still executes once before the condition is checked.
#include <iostream>
int main()
{
int value = 10;
do
{
std::cout << "This runs once" << std::endl;
} while (value < 5);
return 0;
}Even though value < 5 is false, the message still prints once because the loop body is executed before the condition check happens.
Difference Between While and Do While in C++
The main difference lies in when the condition is tested. A while loop checks before the body runs, while a do while loop checks afterward.
| Feature | while | do while |
|---|---|---|
| Condition checked first | Yes | No |
| Condition checked after body | No | Yes |
| Body can run zero times | Yes | No |
| Body runs at least once | No | Yes |
If the logic says “repeat only if the condition is already true,” a while loop fits naturally. If the logic says “do this once, then keep repeating while the condition stays true,” a do while loop is often the better match.
When to Use Do While Loop in C++
- When the loop body must execute at least once.
- When showing a menu before asking whether the user wants to continue.
- When taking input that must be requested once before validation or retry logic repeats it.
- When performing a first action and then checking if another round is needed.
This is why do while is so common in beginner menu programs. The menu must appear at least once, otherwise the user would never see the choices.
Menu Driven Example of Do While Loop
A menu loop is one of the clearest demonstrations of why this loop exists. The menu should show first, the user should make a choice, and then the loop should decide whether to continue.
#include <iostream>
int main()
{
int choice;
do
{
std::cout << "1. Start" << std::endl;
std::cout << "2. Help" << std::endl;
std::cout << "3. Exit" << std::endl;
std::cout << "Enter choice: ";
std::cin >> choice;
std::cout << "You selected: " << choice << std::endl;
} while (choice != 3);
return 0;
}This loop keeps showing the menu until the user enters 3. Since the menu must appear at least once, do while is a very natural fit.
Do While Loop for Input Validation
This loop is also useful when you want to ask for input at least once and keep asking until the entered value becomes valid.
#include <iostream>
int main()
{
int age;
do
{
std::cout << "Enter age: ";
std::cin >> age;
} while (age < 0);
std::cout << "Accepted age: " << age << std::endl;
return 0;
}The prompt must appear at least once, so do while again matches the problem well. The loop continues only while the age remains invalid.
Nested Do While Loop in C++
A do while loop can be placed inside another do while loop. This is called a nested do while loop. It is useful for repeated row and column style logic, small pattern tasks, and multi-level menu structures.
#include <iostream>
int main()
{
int row = 1;
do
{
int col = 1;
do
{
std::cout << "* ";
col++;
} while (col <= 3);
std::cout << std::endl;
row++;
} while (row <= 3);
return 0;
}The outer loop controls rows and the inner loop controls columns. This produces a small 3 by 3 pattern of stars.
Infinite Do While Loop in C++
If the condition never becomes false, the do while loop becomes infinite. This may happen intentionally or by mistake.
do
{
std::cout << "Running forever" << std::endl;
} while (true);Infinite loops can be valid in servers, firmware, and event processing, but in beginner code they are usually unintended. If the state inside the loop never changes, the condition may stay true forever.
Semicolon in Do While Syntax
One special detail beginners should remember is the semicolon after the while (condition) part. Unlike the header of an ordinary while loop, a do while statement ends with a semicolon. Forgetting it causes a syntax error.
This small syntax rule is easy to miss because other loop statements do not use this exact ending pattern. It is worth remembering early so that compiler errors in do while code are easier to understand.
Do While Loop vs For Loop in C++
A for loop is usually chosen when repetition follows a clear counter-based pattern. A do while loop is often better when the code must run once before deciding whether another iteration is needed.
| Feature | for loop | do while loop |
|---|---|---|
| Best for known count | Yes | Possible, but less common |
| Body guaranteed once | No | Yes |
| Header contains init, condition, update | Yes | No |
If a program must print a menu first and then decide whether to repeat, the do while loop often reads more naturally than forcing the same logic into a for loop.
Designing the Condition in a Do While Loop
Since the body runs before the condition is checked, the condition in a do while loop should describe whether another round is needed, not whether the first round is allowed. That is a subtle but important mental model. In menu programs, for example, the loop usually continues while the user has not selected exit. In validation programs, the loop continues while the entered value is still invalid. Thinking in terms of “repeat again if…” helps make the condition easier to write and easier to read later.
Why Do While Fits Retry Style Logic
Retry-style logic is another place where do while feels natural. A program may need to attempt an action once, show the result, and then ask whether it should try again. Because at least one attempt must happen before that decision makes sense, the loop body belongs before the condition check. This is one of the clearest conceptual reasons for using do while instead of forcing the same structure into another loop type.
Common Mistakes with Do While Loop in C++
- Forgetting the semicolon after
while (condition). - Using
do whilewhen the body should not run even once if the condition is false. - Forgetting to update the loop-related state inside the body.
- Writing a condition that never becomes false and creating an infinite loop.
- Using the wrong loop type when a plain
whileorforloop would be clearer.
One common design mistake is choosing do while without thinking about its guaranteed first execution. If the body must be skipped entirely when the condition is initially false, a while loop is the safer choice.
Best Practices for Do While Loop in C++
- Use it when the loop body truly needs to execute at least once.
- Make sure the condition and update logic are easy to understand.
- Use it for menus, retries, and one-time-then-repeat patterns.
- Keep the body readable so the repetition logic stays obvious.
- Remember the required semicolon in the syntax.
Frequently Asked Questions about Do While Loop in C++
Does do while run at least once in C++?
Yes. That is the defining feature of this loop. The body executes before the condition is checked.
When should I use do while instead of while?
Use do while when the logic requires one guaranteed execution before deciding whether to continue, such as menus and retry prompts.
Can a do while loop become infinite?
Yes. If the condition never becomes false, the loop continues forever just like other looping statements.
What is the most common syntax mistake in do while?
Forgetting the semicolon after the while (condition) line is one of the most common syntax mistakes.
Continue learning C++ in order
Follow the topic sequence with the previous and next lesson.