A do-while loop in C++ runs its body first and checks the condition after each pass, so the body always runs at least once. This do while loop in c example program covers the syntax, several complete programs with their output, and the mistakes beginners make most often.
Do-while loop syntax
do {
// statements to repeat
} while (condition);
The do keyword starts the loop, and the braces hold the code that repeats. After the closing brace, while (condition); checks the condition. If the condition is true, the loop runs again. If it is false, the loop ends. The semicolon after the condition is required, and forgetting it is one of the most common errors with this loop.
Example: print the numbers 1 to 5
#include <iostream>
int main() {
int i = 1;
do {
std::cout << i << " ";
i++;
} while (i <= 5);
std::cout << std::endl;
return 0;
}
Output:
1 2 3 4 5
The variable i starts at 1. Each pass prints its value, and the c increment operator i++ adds 1 to it. When i reaches 6, the condition i <= 5 is false, and the loop ends.
Example: count the digits in a number
#include <iostream>
int main() {
int number = 12345;
int digits = 0;
do {
number = number / 10;
digits++;
} while (number != 0);
std::cout << "Digits: " << digits << std::endl;
return 0;
}
Output:
Digits: 5
Each pass divides the number by 10. Because number is an integer, the division drops the last digit. Each pass also counts one digit, and the loop stops once number reaches 0. The != in the condition is one of the c comparison operators.
Try the same code with number set to 0. The body still runs once, so the program reports 1 digit. A while loop with the same check would skip the body and report 0 digits instead.
Example: the body runs once when the condition is false
#include <iostream>
int main() {
int x = 10;
do {
std::cout << "This runs once even though x < 5 is false." << std::endl;
} while (x < 5);
return 0;
}
Output:
This runs once even though x < 5 is false.
A while loop with the same condition would print nothing. This is the key difference between the two loops.
Example: ask for input until it is valid
#include <iostream>
int main() {
int choice = 0;
do {
std::cout << "Enter 1, 2 or 3: ";
std::cin >> choice;
} while (choice < 1 || choice > 3);
std::cout << "You chose " << choice << std::endl;
return 0;
}
Menus and prompts fit this pattern well, because the question has to be asked at least once. This example has a limit, though. If the user types a letter instead of a number, std::cin stops reading, and the prompt repeats forever. Real programs check std::cin.fail(), call std::cin.clear(), and discard the bad input with std::cin.ignore(). The example leaves that out to keep the loop easy to follow.
Do-while vs. while loop
A do-while loop is a while loop with the check moved to the end, so its body runs at least once. For the basic while form, see c while loop syntax.
| while loop | do-while loop | |
|---|---|---|
| When the condition is checked | Before each pass | After each pass |
| Fewest passes possible | Zero | One |
| Typical use | Repeat while something is true | Code that must run at least once |
When to use a do-while loop
Use a do-while loop when the body has to run at least once before the program can decide whether to repeat. Showing a menu, asking for input, and retrying an action that should be tried once are common cases. When you know the exact number of passes ahead of time, a for loop is often clearer. When the check should come before any pass, a while loop usually fits better.
Using break and continue
break leaves the loop right away, the same way it does in any loop. continue skips the rest of the body and jumps to the condition check. The example below prints the numbers 1 to 5 except 3.
#include <iostream>
int main() {
int i = 0;
do {
i++;
if (i == 3) {
continue;
}
std::cout << i << " ";
} while (i < 5);
std::cout << std::endl;
return 0;
}
Output:
1 2 4 5
The i++ comes before the if on purpose. If the increment were placed at the end of the body, continue would skip it when i is 3, and the loop would repeat forever.
Common mistakes
- Missing semicolon.
} while (i <= 5)without the final semicolon does not compile. - Condition that never becomes false. If the body does not change the variable the condition checks, the loop never ends.
- Off-by-one errors. Using
i < 5instead ofi <= 5in the first example prints only 1 to 4. - Variable declared inside the body. A variable declared between the braces is out of scope in the
whilepart, so the condition cannot use it. This causes a compile error. Declare the variable before thedokeyword instead:
int answer = 0;
do {
std::cin >> answer;
} while (answer != 0);
Do-while in C and C++
The do-while syntax is the same in C. What changes is input and output. C uses printf and scanf from stdio.h, while the examples on this page use std::cout and std::cin from iostream.
Summary
A do-while loop runs its body first and checks the condition after, so it always runs at least once. Use it when the first pass must happen before any check. When you know the number of passes in advance, or when the check should come first, a for loop or a while loop is often the better fit.