Iteration
How a program repeats steps: loops that run a set number of times, loops that run until a condition is true, and how to trace both without losing count.
1What iteration is
Iteration is a repeating portion of an algorithm. It repeats either a set number of times (“stir 20 times”) or until a condition is met (“stir until the sugar dissolves”). In the first kind you know the count before you start. In the second, the count is whatever it turns out to be, and it can even be zero.
Plain-language algorithms hide loops in words like “for each”, “keep”, “until”, and “while there are”. Find the part that repeats and the rule that stops it:
| Algorithm in words | Part that repeats | What stops it |
|---|---|---|
| Fill the sink. While there is a dish in the sink, wash one dish and put it on the rack. Drain the sink. | wash one dish, rack it | no dishes left (a condition) |
| Open the attendance list. For each name, read it out and mark present or absent. | read a name, mark it | every name done (a known count) |
| Keep flipping a coin until it lands on heads. | flip the coin | heads shows (a condition) |
“Fill the sink” and “drain the sink” happen once each. Only the middle step repeats, and if the sink starts empty it repeats zero times. That is not a bug; it is what “while there is a dish” means when there are no dishes.
2REPEAT n TIMES
Iteration statements change the normal top-to-bottom order of a program by running a block of statements again and again until a stopping condition is met. The first loop in the reference language counts:
REPEAT n TIMES
{
<block of statements>
}
The block runs n times, no more and no less. Here a loop adds the numbers 1 through 4:
total ← 0
n ← 1
REPEAT 4 TIMES
{
total ← total + n
n ← n + 1
}
DISPLAY(total)
| Pass | total before | n before | total after | n after |
|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 2 |
| 2 | 1 | 2 | 3 | 3 |
| 3 | 3 | 3 | 6 | 4 |
| 4 | 6 | 4 | 10 | 5 |
After the fourth pass the loop is done, so the program displays 10. The variable n is just a regular variable that the block changes; the loop itself keeps its own count of passes.
3REPEAT UNTIL
REPEAT UNTIL (condition)
{
<block of statements>
}
The block repeats until the Boolean expression condition is true. The condition is checked before every pass, including the first. The loop ends the first time the check comes out true.
k ← 1
REPEAT UNTIL (k > 8)
{
k ← k * 2
}
DISPLAY(k)
The program displays 16, not 8. When k is 8, the check 8 > 8 is false, so the block runs once more and doubles k to 16. Counting checks and passes separately is what keeps a trace accurate: for REPEAT UNTIL there is always one more check than there are passes.
4Zero passes and endless loops
Because the condition is checked before the block, two edge cases follow.
Zero passes. If the condition is already true at the first check, the block never runs:
x ← 12
REPEAT UNTIL (x ≥ 10)
{
x ← x + 3
}
DISPLAY(x)
The check 12 ≥ 10 is true right away, so the program displays 12 and the line x ← x + 3 is never executed.
Endless (infinite) loop. If the condition can never become true, the loop never ends:
n ← 5
REPEAT UNTIL (n = 0)
{
n ← n - 2
}
n goes 5, 3, 1, -1, -3, and keeps falling. It jumps over 0, so n = 0 is never true. When you write a REPEAT UNTIL loop, ask: does the block move the variables toward making the condition true, and can it reach it, not skip past it?
REPEAT n TIMES runs its block n times. REPEAT UNTIL (condition) checks the condition before each pass and stops the first time it is true, so the block can run zero times, and runs forever if the condition can never become true.
5Tracing a loop
To find what a loop does, make a table with one row per check (for REPEAT UNTIL) or per pass (for REPEAT n TIMES), and a column for every variable that changes. Fill in the row, then do what the row says. Don't skip rows in your head; most wrong answers are one pass too many or too few.
The two loops can do the same job. A REPEAT UNTIL with a counter behaves like REPEAT 3 TIMES:
i ← 0
REPEAT UNTIL (i = 3)
{
DISPLAY("hi")
i ← i + 1
}
Both versions display hi three times. The counter starts at 0, goes up by 1 each pass, and the loop stops when it reaches 3. Start the counter at 1 instead and you get only two passes.
6Loops inside loops
A loop's block can contain another loop. The inner loop then runs all the way through on every pass of the outer loop:
count ← 0
REPEAT 3 TIMES
{
REPEAT 4 TIMES
{
count ← count + 1
}
}
DISPLAY(count)
The outer loop makes 3 passes, and each pass runs the inner block 4 times, so count goes up 3 × 4 = 12 times. Put the same two loops one after the other instead, and the counts add:
count ← 0
REPEAT 3 TIMES
{
count ← count + 1
}
REPEAT 4 TIMES
{
count ← count + 1
}
DISPLAY(count)
This version displays 7, which is 3 + 4. A quick way to remember it: nesting multiplies, one after the other adds.
7Worked examples
Subtract until small
What is displayed?
count ← 0
value ← 50
REPEAT UNTIL (value < 10)
{
value ← value - 15
count ← count + 1
}
DISPLAY(count)
DISPLAY(value)
- 1Make one row per check of
value < 10:Check value value < 10? What happens 1 50 false subtract 15, add 1 to count 2 35 false subtract 15, add 1 to count 3 20 false subtract 15, add 1 to count 4 5 true stop - 2The block ran 3 times, so
countis 3. The last value ofvalueis 5.
3 then 5. Four checks, three passes.Saving up
Mina has $200 saved and adds $45 each week. Write a loop that finds how many weeks until she has at least $500, and say what it displays.
- 1The number of weeks is not known in advance, so use
REPEAT UNTIL. - 2Stop when
balance ≥ 500; each pass adds 45 and counts one week.balance ← 200 weeks ← 0 REPEAT UNTIL (balance ≥ 500) { balance ← balance + 45 weeks ← weeks + 1 } DISPLAY(weeks) - 3Balances at each check: 200, 245, 290, 335, 380, 425, 470, 515. The check first comes out true at 515.
7: after 7 weeks she has $515.Which displays 1 2 4 8?
Which of these segments display 1 2 4 8?
Segment A
x ← 1
REPEAT 4 TIMES
{
DISPLAY(x)
x ← x * 2
}
Segment B
x ← 1
REPEAT 4 TIMES
{
x ← x * 2
DISPLAY(x)
}
Segment C
x ← 1
REPEAT UNTIL (x > 8)
{
DISPLAY(x)
x ← x * 2
}
- 1A displays before doubling:
1 2 4 8. - 2B doubles before displaying, so the first value shown is 2:
2 4 8 16. - 3C displays while
xis 1, 2, 4, 8; whenxbecomes 16 the check16 > 8is true and it stops:1 2 4 8.
Odd numbers forever
This segment should display the odd numbers from 1 to 9, but it never stops. Why, and how do you fix it?
count ← 1
REPEAT UNTIL (count = 10)
{
DISPLAY(count)
count ← count + 2
}
- 1
counttakes the values 1, 3, 5, 7, 9, 11, … It is always odd, so it is never equal to 10. - 2The condition can never be true, so the loop is endless.
- 3Change the condition to one the loop cannot jump over:
count ← 1 REPEAT UNTIL (count > 10) { DISPLAY(count) count ← count + 2 }
count > 10 the segment displays 1 3 5 7 9 and stops.8Common mistakes
For REPEAT UNTIL (k > 8), the loop does not stop at 8: 8 > 8 is false. Check the condition with the actual value, every time.
REPEAT UNTIL (x = 5) keeps going while x is not 5. It stops when the condition becomes true, not when it becomes false.
If the condition is true at the first check, or n is 0 in REPEAT n TIMES, the block never runs and every variable keeps its starting value.
Counting by 2 from 1 never hits 10, and counting down by 2 from 5 never hits 0. Use >, ≥, <, or ≤ when a variable moves in steps bigger than 1.
“The loop repeats until value < 10. The condition is checked before each pass: it is false at 50, 35, and 20, so the block runs three times, and true at 5, so the loop stops. The segment displays 3 and then 5.”
Name the stopping rule, give the value at each check, and say how many times the block ran.