Repeat While a Condition Holds
Trace a while loop through its starting state, condition, body, update, and final false check. Repair a missing update and test zero, one, and boundary iterations.
A for loop is clearest when a known sequence or repetition count determines
the work. A while loop is useful when a changing condition determines when
to stop.
Check Before Each Iteration
Suppose a sensor needs three calibration checks:
Python follows the same cycle repeatedly:
- evaluate
check < 3; - if it is
True, run the indented body; - update
check; - return to the condition.
| Condition check | Body runs with check | Value after update |
|---|---|---|
0 < 3 → True | 0 | 1 |
1 < 3 → True | 1 | 2 |
2 < 3 → True | 2 | 3 |
3 < 3 → False | — | 3 |
The body runs three times. The final false check is part of the trace even though it produces no output.
Q1. Trace the stopping check
What does this program display?
Select one choice, then check.
HintInclude the boundary
The operator <= asks whether step is less than or equal to 3.
SolutionThe loop displays 1 through 3
The body runs for 1, 2, and 3. After the update to 4, 4 <= 3 is false.
Zero, One, and Several Iterations
Because the condition is checked first, a while loop may run zero times:
The starting condition is false, so the body is skipped. Starting at 2 would produce one iteration; starting at 0 produces three. Testing these small cases reveals boundary mistakes quickly.
This loop stops before printing 3:
If 3 must be included, repair the condition as step <= 3. The first false
check then occurs at 4, immediately after the intended final value.
Repair a Loop That Cannot Finish
This loop never changes the value used by its condition:
Once the condition is true, it remains true. Add the intended update inside the body:
check = check + 1
An infinite loop is not repaired by guessing a different comparison. Identify the state named by the condition, then verify that the body changes it toward a false condition.
Q2. Repair the missing update
Repair the loop so it displays 0, 1, and 2, then stops.
Editable Python
Ready to run.
HintMove toward the stop
Add step = step + 1 at the same indentation as print(step).
SolutionUpdate after each printed value
Choose the Loop from the Stopping Rule
Use for reading in readings when every value in a known sequence should be
visited. Use while when the next iteration depends on changing state and the
number of iterations is not naturally supplied by a sequence.
Q3. Choose for or while
A calibration repeats until difference <= 0.1. The number of checks is not
known beforehand. Which loop matches that stopping rule?
Select one choice, then check.
HintAsk what determines the end
The number of repetitions becomes known only while calibration runs.
SolutionUse while for the changing condition
Continue while the difference is above 0.1 and update the difference during each iteration so the condition can eventually become false.
A while trace must show starting state, condition, body, and update. Checking
zero, one, and boundary iterations reveals loops that stop too early, too
late, or not at all. The final lesson distinguishes skipping one iteration
from leaving a loop before later values are visited.