The do-while Loop
The do-while loop is like while, but it guarantees the body executes at least once before checking the condition.
int x = 10;
do {
cout << x << "\n";
x--;
} while (x > 0);
This is perfect when the first iteration must always happen โ for example, showing a menu before asking the user to pick an option.
The condition comes after the body, so remember the semicolon at the end.
int secret = 7;
int guess;
do {
cout << "Guess: ";
cin >> guess;
} while (guess != secret);
cout << "Correct!";
Try it Yourself โ