6. Repeating: loops
The temperature program at the end of the last chapter copied the same if three times just to change one number. Repetition is what computers are best at: give them a rule and let them turn the crank. This chapter has two kinds of crank: while and for.
In this chapter you will learn:
- to repeat while a condition holds, with
while; - to follow a loop with a variable trace table;
- to repeat a counted number of times with
forandrange; - to walk through the pieces of a string or a list with
for; - to leave a loop early with
break, skip to the next round withcontinue, spin withwhile true, and leave a branch empty withpass; - to combine all of it into FizzBuzz.
6.1 while: repeat while the condition holds
var count = 3
while count > 0:
print(count)
count -= 1
print("liftoff")3
2
1
liftoffwhile condition: looks like if, except that it does not look only once:
- Check
count > 0.countis 3, true, so run the indented block. - The block prints
3, then subtracts 1 fromcount, which becomes 2. - Go back to step 1 and check again. This prints
2and1;countbecomes 0. - The next check is
0 > 0, false, so the loop is left. print("liftoff")is not indented, so it is outside the loop and runs after it.
The loop stops because the block contains a line that eventually makes the condition false — here count -= 1. Without it, count would stay 3 and the program would print forever and never stop; press Ctrl+C in the terminal to force it to end.
6.2 A variable trace table
Loops are hard to read by eye, and the steadiest way is to draw a table and follow it step by step. This program adds 1 through 5:
var total = 0
var n = 1
while n <= 5:
total += n
n += 1
print(total)15The trace, one row per round:
| Round | n when checked | n <= 5 | total after adding | n after adding |
|---|---|---|---|---|
| before the loop | 1 | — | 0 | 1 |
| 1 | 1 | true | 1 | 2 |
| 2 | 2 | true | 3 | 3 |
| 3 | 3 | true | 6 | 4 |
| 4 | 4 | true | 10 | 5 |
| 5 | 5 | true | 15 | 6 |
| after | 6 | false | 15 | 6 |
total ends at 15: 1+2+3+4+5. When a loop of your own gets stuck, take a sheet of paper and write down "the values in the condition, the values in the key variables" for each round, just like this.
6.3 Deliberate mistake: a while condition that is not a Bool
let n = 3
while n:
print(n)SPR-TYPE-CONDITION [TYPE] main.spr:2:7: Condition must be Bool (no truthiness) (expected Bool, actual Int)Chapter 3 said it: Sprig has no "non-zero is true". Here n is an Int, so the compiler asks for a real comparison: while n > 0:. The same rule applies to if.
6.4 for and range: repeating a count
When you know what to count and where to stop, for is less work:
for n in range(3):
print(n)
print("--")
for n in range(2, 6):
print(n)
print("--")
for n in range(0, 10, 3):
print(n)
print("--")
for n in range(5, 0, -2):
print(n)0
1
2
--
2
3
4
5
--
0
3
6
9
--
5
3
1for variable in a sequence of values: takes those values one at a time and runs the block once for each. range produces integers:
range(3): starts at 0 and stops before 3, giving 0, 1, 2. The stop value is excluded.range(2, 6): from 2 up to but not including 6, giving 2, 3, 4, 5.range(0, 10, 3): the third argument is the step; it adds 3 each time, giving 0, 3, 6, 9.range(5, 0, -2): the step may be negative; counting down from 5 gives 5, 3, 1.
range takes different numbers of arguments: one means "from 0 up to this number", two mean "start, stop", and three add the step.
6.5 Deliberate mistake: the loop variable cannot change
The loop variable is replaced by the next value each round, so it behaves like a let:
for n in range(3):
n += 1SPR-NAME-LET-ASSIGN [TYPE] main.spr:2:5: Cannot assign to immutable binding 'n'; declare it with varWhen you want a value that is computed from the loop variable and changes each round, declare a var inside the block:
for n in range(3):
var doubled = n * 2
doubled += 1
print(doubled)1
3
5n takes 0, 1 and 2; doubled is declared fresh each round and comes out as 1, 3 and 5. The loop variable does not live outside the loop either. A print(n) after the loop cannot find n, and the compiler reports SPR-NAME-UNRESOLVED.
6.6 Walking through strings and lists
for is not only for counting; the "sequence of values" on its right can be anything that can be taken one piece at a time. A string, met in chapter 4, is a sequence of characters:
for ch in "Sprig":
print(ch)S
p
r
i
gEach round ch receives one character, as a String holding exactly one character (counted in code points, as chapter 4 said), and it is immutable inside the loop just like the counting variable.
Square brackets wrap a list literal, which chapter 8 covers in full; for treats it the same way as a string:
for word in ["tea", "milk", "rice"]:
print(word)tea
milk
rice6.7 break and continue
continue: abandon this round and start the next one.break: stop the whole loop; no later round runs.
for n in range(1, 10):
if n % 2 == 0:
continue
if n > 7:
break
print(n)1
3
5
7Round by round:
n | What happens |
|---|---|
| 1 | odd; 1 > 7 is false; print 1 |
| 2 | even, continue, back to the top of the loop |
| 3 | print 3 |
| 4 | continue |
| 5 | print 5 |
| 6 | continue |
| 7 | print 7 |
| 8 | even, so continue runs before the break check is ever reached |
| 9 | odd; 9 > 7 is true, break, the loop ends |
Notice the order: round 8 is caught by continue before the break check. Do not make the two exits of a loop too tangled.
6.8 Deliberate mistake: break outside a loop
breakSPR-FLOW-BREAK [FLOW] main.spr:1:1: break outside a loopbreak and continue may only live inside a loop body. The compiler performs flow analysis (the category is FLOW) and sees at once that this line has no loop to leave. A continue outside a loop reports SPR-FLOW-CONTINUE, by the same rule.
6.9 while true and pass
Some loops do not have a tidy condition to put at the top, such as "find the first number divisible by 7": let while true: spin, and break when the search succeeds.
pass is the statement that does nothing. A branch's block must contain at least one line, so when there is really nothing to do, pass fills the spot:
var answer = 0
while true:
answer += 1
if answer % 7 == 0:
break
print(answer)
for n in range(5):
if n == 2:
pass
else:
print(n)7
0
1
3
4The first half: answer grows from 0 by 1 each round; at 7, 7 % 7 == 0 holds, so it breaks and prints 7. With while true, always make sure a break is somewhere inside, or it really will spin forever.
The second half: range(5) yields 0 through 4. When n == 2 there is nothing to do, but the block cannot be empty, so pass holds its place; the other values are printed by the else block, which is why 2 is missing from the output.
6.10 Putting it together: FizzBuzz
Combine for, if, elif and % into a classic: count from 1 to 15; print Fizz for multiples of 3, Buzz for multiples of 5, FizzBuzz for multiples of both, and the number itself otherwise.
for n in range(1, 16):
if n % 15 == 0:
print("FizzBuzz")
elif n % 3 == 0:
print("Fizz")
elif n % 5 == 0:
print("Buzz")
else:
print(n)1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzzA number divisible by both 3 and 5 is also divisible by each of them, so n % 15 == 0 must come first; otherwise 15 is caught by n % 3 == 0 and printed as Fizz. Branches are checked from top to bottom and the first match wins — the rule from the last chapter.
Summary
while condition:checks once at the top of every round; the block must eventually make the condition false.- Read a loop with a trace table: list the condition and the key variables for each round.
- A condition must be a
Bool; there is no "non-zero is true". for x in a sequence:takes one value at a time; all three forms ofrangeexclude the stop value, and the step may be negative.- The loop variable behaves like a
let, cannot be assigned, and does not exist after the loop. continuestarts the next round,breakends the whole loop, and both may only appear inside a loop.while truepairs withbreak;passis the empty statement.
Exercises
Exercise 1: a table of squares
Print the squares of 1 through 10 with for and range, one per line.
Hint: for n in range(1, 11): and print n * n in the block.
Answer
for n in range(1, 11):
print(n * n)1
4
9
16
25
36
49
64
81
100Exercise 2: factorial with while
Compute the factorial of 5 (5×4×3×2×1) with while and print the result.
Hint: start with var n = 5 and var result = 1; each round do result *= n, then n -= 1, while n > 1.
Answer
var n = 5
var result = 1
while n > 1:
result *= n
n -= 1
print(result)120Exercise 3: countdown
Count from 5 down to 1 with range, one per line, then print liftoff.
Hint: the step is -1; the start is 5 and the stop is 0 (because the stop is excluded).
Answer
for n in range(5, 0, -1):
print(n)
print("liftoff")5
4
3
2
1
liftoffExercise 4: counting vowels
How many vowels (a, e, i, o, u) are in "banana"? Print the count.
Hint: var vowels = 0, for ch in "banana":, and join all the vowels into one condition with or.
Answer
var vowels = 0
for ch in "banana":
if ch == "a" or ch == "e" or ch == "i" or ch == "o" or ch == "u":
vowels += 1
print(vowels)3Exercise 5: the first square above 200
Counting up from 1, which number's square is the first to exceed 200? Stop as soon as you find it with break and print the number.
Hint: keep the answer in var found = -1; in the loop, test n * n > 200, and when it holds store n in found and break.
Answer
var found = -1
for n in range(1, 100):
if n * n > 200:
found = n
break
print(found)15Coming from another language?
- Python:
while,for ... in,range,breakandcontinuelook and behave almost the same, andrangeexcludes the stop value there too. - Java / JavaScript / C: there is no
i++or++i— writei += 1; there is no C-stylefor (i = 0; i < n; i++), so counting usesrange; there is nodo ... while; and nofor(;;), so writewhile true. - The loop variable is gone after the loop, unlike Python: here it is created fresh in each round.
Next chapter: Chapter 7: Functions.
