4.1.1.2 - Definite, indefinite and nested iteration (Python)
Choose a suitable loop, trace its changing state, and combine loops with selection, including pre-test, post-test and nested repetition.
Definite iteration and loop state
Iteration repeats statements. Definite iteration has a known number of repetitions, such as reading exactly five measurements. A Python for loop over range() is useful when the required count is known.
total = 0
for number in range(1, 4):
total = total + number
print(number, total)
range(1, 4) supplies 1, 2 and 3: the stop value 4 is excluded. The output is 1 1, 2 3, 3 6. The accumulator total starts at zero before the loop and retains the previous sum. Initialising it inside the loop would discard previous contributions. The loop variable receives each supplied value automatically.
range(5) supplies 0 through 4 and gives five repetitions. range(6, 0, -2) supplies 6, 4 and 2. The third argument is the step; it must not be zero. If the range is empty, the body does not run. A counter counts events; an accumulator combines their values. Both need appropriate initial values.
Pre-test indefinite iteration
Indefinite iteration repeats until a condition changes, so the number of repetitions is not known in advance. A while loop is pre-test: it checks the condition before every execution of its body and can execute zero times.
count = 0
value = int(input())
while value != -1:
count = count + 1
value = int(input())
print(count)
Here -1 is a sentinel, a special value signalling the end. The first input establishes the state before the condition is checked. Each accepted value increases the count and the next input refreshes the state. The sentinel is not counted. Inputs 8, 3, -1 print 2; an initial -1 prints 0. Removing the input inside the loop would leave a non-sentinel value unchanged and create an infinite loop.
When designing a loop, identify its initial state, condition, body and progress toward termination. Use the condition's meaning carefully: while value != -1 continues while the value is not the sentinel. A pre-test validation loop may need an initial read before its test. The repeated statements must be indented inside the loop.
Post-test repetition and Python equivalents
A post-test loop runs its body before checking its termination condition, so it executes at least once. The distinction is required even though Python has no built-in REPEAT ... UNTIL statement.
In this supplied pseudocode convention, UNTIL gives a stopping condition:
REPEAT
INPUT code
UNTIL code = "open"
The input occurs once before any test. The loop stops when the comparison is true. A continuation condition would have the opposite sense. Python can implement the same behavior using an unconditional loop and an explicit exit:
while True:
code = input()
if code == "open":
break
print("accepted")
break exits the nearest enclosing loop. Inputs wrong, open cause two reads and then print accepted; an initial open still causes one read. The code combines iteration, selection and sequence. This equivalence is about when the condition is checked, not about pretending Python has native post-test syntax.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account