C++ do-while Loop
In this page:
do {
// body runs at least once
} while (condition);
Basic do-while Loop
A do-while loop is a post-test loop: it runs the body first and only checks the condition afterward, at the bottom of the loop, which is the opposite order from a regular while loop's condition-first check.
This structural difference is what guarantees the loop body always executes at least once.
Example: Basic do-while Loop
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare count and set it to 0
int count = 0;
// do-while: run the body once, then repeat while the condition is true
do {
// Print to the console with cout
std::cout << count << std::endl;
count++;
} while (count < 5);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Guaranteeing At Least One Run
Unlike a while loop, which might never run its body if the condition starts false, a do-while loop always executes at least one full pass before the condition is even evaluated.
This makes it the right structural choice whenever 'do this at least once, then repeat if needed' is genuinely what the logic calls for.
Example: Guaranteeing At Least One Run
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare count and set it to 10
int count = 10;
// do-while: run the body once, then repeat while the condition is true
do {
// Print to the console with cout
std::cout << "Runs once even though condition is false" << std::endl;
count++;
} while (count < 5);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Simulating User Menu Choices
Interactive menus are a textbook use case for do-while, since you naturally want to display the menu and get the user's first choice before there's anything to check — the loop then repeats showing the menu until the user picks an exit option.
A regular while loop would need an awkward workaround to show the menu before its condition could even be evaluated.
Example: Simulating User Menu Choices
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int choice;
// Declare attempts and set it to 0
int attempts = 0;
int hardcodedInputs[] = {5, 2};
// do-while: run the body once, then repeat while the condition is true
do {
choice = hardcodedInputs[attempts];
// Print to the console with cout
std::cout << "Menu choice: " << choice << std::endl;
attempts++;
} while (choice != 2);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Data Validation Simulation
Input validation follows the same pattern: you have to actually prompt for and receive input before you can check whether it's valid, which is exactly the 'act first, check after' order a do-while loop provides.
This avoids needing to fake an initial value just to satisfy a while loop's upfront condition check.
Example: Data Validation Simulation
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int age;
int hardcodedInputs[] = {-5, 25};
// Declare attempts and set it to 0
int attempts = 0;
// do-while: run the body once, then repeat while the condition is true
do {
age = hardcodedInputs[attempts];
// Print to the console with cout
std::cout << "Entered age: " << age << std::endl;
attempts++;
} while (age < 0);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested do-while Loops
Nesting do-while loops inside each other works the same way as nesting any other loop type, and is useful for building grid-based patterns or processing multi-dimensional data where each dimension needs to run at least once regardless of any condition, such as generating a fixed-size table.
Example: Nested do-while Loops
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare i and set it to 1
int i = 1;
// do-while: run the body once, then repeat while the condition is true
do {
// Declare j and set it to 1
int j = 1;
// do-while: run the body once, then repeat while the condition is true
do {
// Print to the console with cout
std::cout << i << "," << j << " ";
j++;
} while (j <= 2);
// Print to the console with cout
std::cout << std::endl;
i++;
} while (i <= 2);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting the semicolon after
while (condition)at the end of a do-while, which causes a compile error. - Forgetting that the body runs at least once even if the condition is false.
- Never changing the variable in the condition, creating an infinite loop.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: