6.2.2 – Condition-controlled loops and iteration
Control repetition with changing state, process input until a sentinel, and iterate directly over stored values. Explain zero-pass cases and show why a loop terminates.
A condition controls repetition
A condition-controlled loop repeats while its condition is True. Python's while is pre-conditioned: the condition is checked before every pass, including the first. The body can therefore run zero times.
spaces_left = 12
package_size = 5
while spaces_left >= package_size:
print("Pack placed")
spaces_left = spaces_left - package_size
print("Spaces left:", spaces_left)
| Value at check | Condition | Action | New spaces_left |
|---|---|---|---|
| 12 | True | Place a package | 7 |
| 7 | True | Place a package | 2 |
| 2 | False | Exit loop | 2 |
Pack placed
Pack placed
Spaces left: 2
The update changes a value used by the condition, making termination possible. Starting with 4 spaces gives zero passes; starting with exactly 5 gives one pass and zero spaces. A positive package size is required for the intended progress. The initial state, condition and update must work together.
Make progress towards termination
An infinite loop has no normal termination for the current state. A missing update can leave a True condition unchanged:
tickets_left = 3
while tickets_left > 0:
print("Ticket issued")
The value stays 3, so the loop repeats indefinitely. A decrement inside the body gives states 3, 2, 1, then 0 at the checks:
tickets_left = 3
while tickets_left > 0:
print("Ticket issued")
tickets_left = tickets_left - 1
print("Closed")
Placing the update after the loop cannot help the original loop terminate, because control never reaches it. Changing the value in the wrong direction may also prevent termination. Do not infer that any assignment inside a loop is sufficient: it must make the condition capable of becoming False.
The condition controls the structured exit. These examples use no break or continue; the required progression is visible in the header and body. Use Stop if an experiment loops unexpectedly, then inspect the state change rather than repeatedly running it.
Read until a sentinel
A sentinel is a special value that ends input rather than representing ordinary data. A program may not know how many readings the user will supply. Ask once before the condition, process a valid reading in the body, then refresh it for the next check.
total = 0
reading = int(input("Reading, or -1 to stop: "))
while reading != -1:
total = total + reading
reading = int(input("Reading, or -1 to stop: "))
print("Total:", total)
Here ordinary readings are non-negative integers, so -1 is available as the sentinel. It is tested before the addition and is never included in total. A first entry of -1 gives zero passes and total zero.
Two readings
Example runReading, or -1 to stop: 4
Reading, or -1 to stop: 6
Reading, or -1 to stop: -1
Total: 10
Omitting the second input would repeatedly process the first reading. Placing the addition after the refreshed input could accidentally count the sentinel. Trace the sequence input → test → process → refresh, and keep its purpose clear. This is a termination protocol, not general validation of malformed numeric text; suitable integer entries are assumed.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account