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:

  1. evaluate check < 3;
  2. if it is True, run the indented body;
  3. update check;
  4. return to the condition.
Condition checkBody runs with checkValue after update
0 < 3True01
1 < 3True12
2 < 3True23
3 < 3False3

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?

Choose one

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.

Not attempted
Review

Not marked done.

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

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintMove toward the stop

Add step = step + 1 at the same indentation as print(step).

SolutionUpdate after each printed value
Not attempted
Review

Not marked done.

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?

Choose one

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.

Not attempted
Review

Not marked done.

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.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.