Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
A while loop tests a Boolean condition before it runs its body. In the countdown, count starts at 3, so the condition count > 0 is true and the body runs. The body prints count and then decreases it. After the body finishes, execution returns to the condition instead of moving directly to the line after the loop.
int count = 3;
while (count > 0) {
cout << count << "\n";
count--;
}
cout << "Done\n";The line while (count > 0) contains the condition. The statements between the braces are the loop body. The exact order is: test count > 0, run the body only when the test is true, then return to the test. When the test is false, the body is skipped and execution continues with cout << "Done\n".
Which action happens first in the countdown?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The first condition check sees count equal to 3, so the body prints 3 and count-- changes count to 2. The next check sees 2, prints it, and changes count to 1. The next check sees 1, prints it, and changes count to 0. Each pass changes the value that the next condition check will read.
| COUNT AT CHECK | COUNT > 0 | BODY ACTION | COUNT AFTER BODY |
|---|---|---|---|
| 3 | true | Print 3, then count-- | 2 |
| 2 | true | Print 2, then count-- | 1 |
| 1 | true | Print 1, then count-- | 0 |
| 0 | false | Body does not run | 0 |
The value in the first column is the value available before the condition is tested. The value in the last column is the value left for the next check. The post-decrement does not change the already printed number, so the body prints 3 before changing count to 2, then prints 2 before changing count to 1.
The while statement supplies repetition, not progress. The body must change something that can eventually make the condition false. If the decrement is removed, the body still prints count, but nothing changes count. The first check sees 3, the body prints 3, and the next check sees 3 again.
int count = 3;
while (count > 0) {
cout << count << "\n";
}
cout << "Done\n";This loop does not reach the line that prints Done. It keeps taking the true path because count remains 3, so it keeps printing 3. The loop does not automatically decrement, increment, or otherwise update a control variable. Those changes must be written in the body.
Complete the countdown body by typing the missing progress line.
while (count > 0) {
cout << count << "\n";
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
After the body prints 1, count-- changes count from 1 to 0. The loop then performs one final condition check. Because count > 0 is false when count is 0, the body does not run again, so 0 is not printed. Execution leaves the loop and prints Done, with count still equal to 0.
condition check: count = 3, true
print: 3
count becomes 2
condition check: count = 2, true
print: 2
count becomes 1
condition check: count = 1, true
print: 1
count becomes 0
condition check: count = 0, false
print: DoneThe final failed check is still part of the loop's control flow, but it is not another repetition. A true check permits one body pass. A false check skips the body and sends execution to the first statement after the loop. That is why the output ends with Done rather than 0.