While And Do-While Loops
Some tasks need to repeat, but you might not know how many times until the program is actually running.
For example, you might build a game that asks the user to enter a guess. If they get it wrong, it asks again. And again, as many times as needed.
That’s what loops are for. This lesson covers two loop types: while and do-while.
The while loop
A while loop has this form:
while (condition) { // The code that needs to repeat}The structure is simple: while followed by a condition in parentheses, then a block of code in braces, called the loop’s body.
Here’s what happens at each step:
- The condition is evaluated first. If it’s
true, the body of the loop runs. Then, the condition is re-evaluated for another pass. - This keeps going until the condition becomes
false, at which point the body of the loop is skipped and the program moves on.
For example, the following program keeps asking for a number until the user enters something positive:
Here, the loop keeps running as long as the condition (amount <= 0) holds. The moment amount becomes positive, the condition turns false, and the loop stops.
Zero iterations is normal
A while loop might run zero times, if the condition is false to begin with. If the user had entered 10 on their very first try, the loop’s body would never run at all, because 10 <= 0 is false.
The do-while loop
do-while flips the order: run the loop’s body first, then check the condition.
do { // The code that needs to repeat} while (condition);If the condition is true, go back and run the loop’s body again. If it’s false, stop.
For example, say you’re validating a menu choice, and you want to show the menu at least once no matter what:
This keeps showing the menu and re-checking the choice until the user enters something between 1 and 3.
When to use which
Depending on which order you need, one of these loops will be a better fit:
- Use
do-whilewhen the task needs to happen at least once regardless of any condition, like showing a menu. - Use plain
whilewhen it’s entirely possible you won’t need to run the loop’s body at all, like the deposit example, where the user might get it right on the first try.
A common mistake: forgetting the semicolon
One small thing to watch for with do-while: it ends with a semicolon after the condition, unlike while.
do { // ...} while (choice < 1 || choice > 3); // <- semicolon required hereWatch out for loops that never end
For a loop to end, something inside its body has to change, something that’ll eventually make the condition false. If nothing changes, the loop runs forever.
int count = 0;while (count < 5) { std::cout << count << "\n"; // forgot to update count!}This prints 0 forever, because count never changes. A loop like this is called an infinite loop, and is a common bug for beginners.
Checkpoint
A car has just pulled up to a parking garage gate. The gate must display “Please pay before exiting” right away, then keep displaying this until the driver scans a valid ticket. Which loop is a better fit for this?
Given this code, what is printed?
int fuel = 0;
while (fuel < 100) {
fuel += 25;
std::cout << fuel << "\n";
}How many times will this snippet print?
int attempts = 0;
do {
attempts++;
std::cout << "Attempt #" << attempts << "\n";
} while (attempts < 3)