3.2.2 - Condition-controlled iteration (Python)
Some tasks repeat until an event occurs rather than for a known count. You will compare conditions tested before and after the body, then reason about how a loop reaches its stopping state.
Testing before repetition
A start-tested loop checks its condition before every possible execution of the body.
Python's while repeats while its condition is true. If it is false on the first check, the body never runs.
balance = 12
while balance >= 5:
balance = balance - 5
print(balance)
With 12 initially, the first update leaves 7 and the second leaves 2. The next condition is false, so the output is 7 then 2. Starting with 4 would produce no output. The condition is checked again after the entire indented body, not after every statement. A Boolean combination such as attempts < 3 and not accepted repeats only while both requirements hold.
This is indefinite iteration because a condition determines whether another repetition happens. The programmer does not specify a fixed number of body executions in the loop statement.
Testing after repetition
An end-tested loop executes its body once before checking whether to repeat.
This is useful when a user must be asked at least once. In AQA supplied pseudocode, REPEAT ... UNTIL stops when its final condition becomes true:
REPEAT
OUTPUT "Try a code"
code ← USERINPUT
UNTIL code = "OPEN"
The related DO ... WHILE form continues while its final condition is true, so the matching continuation condition is code ≠ "OPEN". These forms both test at the end, but the stopping and continuing conditions are opposites.
Python has no direct repeat until statement. A loop with an unconditional true condition and an explicit exit can implement the same behaviour:
while True:
code = input("Code: ")
if code == "OPEN":
break
print("Accepted")
break exits the nearest loop. The input runs before the stopping test, so at least one input is obtained. When asked to read supplied pseudocode, use its stated convention; when asked for Python, write native Python rather than inserting pseudocode keywords.
Ask at least once
{"code":"while True:\n code = input("Code: " )\n if code == "OPEN":\n break\nprint("Accepted")","instructions":"Predict and run with CLOSED then OPEN. Change the accepted code from OPEN to START and run with OPEN then START. Observe that the old code now requires another input."}
Updating the condition and terminating
A condition-controlled loop needs a path that eventually makes its continuation condition false or reaches an explicit exit.
The balance example terminates because each repetition reduces the balance by 5. If the update were omitted, a starting balance of 12 would remain 12 and the loop would never stop. In an input loop, obtain a fresh input inside the repeated body: repeatedly comparing the same rejected value makes no progress.
To reason about correctness, identify the starting state, which statements change the relevant value and the stopping condition. A countdown starting at 3, displaying its value and subtracting one while the value is positive displays 3, 2, 1. It then reaches zero, where the condition fails. Indentation must keep the update inside the body.