do-while Loop in C++
do-while is while with the test moved to the bottom: the body always runs at least once, then repeats while the condition holds. That single guarantee makes it the loop for menus and prompts — show the menu first, then ask whether to continue.
The syntax quirk is the trailing semicolon (} while (...);), easy to forget and odd-looking until it becomes habit. Prefer plain while when zero iterations must be possible; reach for do-while exactly when the first pass is unconditional.
Body first, test after
do { ... } while (cond); runs the block, then checks cond — one execution is guaranteed no matter what.
Don't forget the semicolon
The closing }; is mandatory and unique among loops — missing it is a compile error pointing at a confusing line.
Menus are the use case
Display options, read a choice, repeat while the choice isn't quit — the prompt must appear before any test can decide.
while vs do-while
Zero-or-more iterations means while; one-or-more means do-while. Choosing by that sentence never goes wrong.
#include <iostream> int main() { int choice = 0; int shows = 0; do { ++shows; choice = 9; } while (choice != 9); std::cout << "shown " << shows << " time(s)" << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.