C++ do-while Loop
In this page:
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 <iostream>
int main() {
int count = 0;
do {
std::cout << count << std::endl;
count++;
} while (count < 5);
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 <iostream>
int main() {
int count = 10;
do {
std::cout << "Runs once even though condition is false" << std::endl;
count++;
} while (count < 5);
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 <iostream>
int main() {
int choice;
int attempts = 0;
int hardcodedInputs[] = {5, 2};
do {
choice = hardcodedInputs[attempts];
std::cout << "Menu choice: " << choice << std::endl;
attempts++;
} while (choice != 2);
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 <iostream>
int main() {
int age;
int hardcodedInputs[] = {-5, 25};
int attempts = 0;
do {
age = hardcodedInputs[attempts];
std::cout << "Entered age: " << age << std::endl;
attempts++;
} while (age < 0);
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 <iostream>
int main() {
int i = 1;
do {
int j = 1;
do {
std::cout << i << "," << j << " ";
j++;
} while (j <= 2);
std::cout << std::endl;
i++;
} while (i <= 2);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: