GoalTell apart a count-controlled loop and a condition-controlled loop, and trace how many times each one runs.
New words
iteration · repeating a section of a program, also called a loopcount-controlled loop · a loop that repeats a set number of timescondition-controlled loop · a loop that repeats while a condition is true, and stops when it becomes falseinfinite loop · a loop that never stops, because its condition never becomes false
Explanation
Iteration means repeating blocks. It saves us from writing the same blocks again and again. A count-controlled loop, such as repeat 4 times, runs a set number of times. We know the number before the program starts. A condition-controlled loop, such as while n < 50, runs for as long as its condition is true. The condition is checked before every round, so if it is false at the start, the loop does not run at all. Something inside the loop must change the variable, or the condition never becomes false and the loop never ends. To trace a loop, write the variables in a table, one row for each round.
Worked examples
Round
total before
total after
1
0
3
2
3
6
3
6
9
4
9
12
Trace the count-controlled loop from the explanation. The blocks inside run exactly 4 times, so total goes up by 3 each time. The board shows 12.
This condition-controlled loop doubles n until n is no longer less than 50. The condition is checked before each round. The loop runs 6 times and the board shows 64.
Round
n before
n < 50?
n after
1
1
true
2
2
2
true
4
3
4
true
8
4
8
true
16
5
16
true
32
6
32
true
64
check
64
false
stop
Trace of the doubling loop. Notice that n = 32 is less than 50, so there is one more round. After it n = 64, the condition is false and the loop stops.
Class activity
Human loops (no computer): one player is the computer and the others watch. Run a count-controlled loop: ‘repeat 6 times: take a step forward and say the step number’. Then run a condition-controlled loop: put 5 sheets of paper in a pile and follow ‘while the pile has fewer than 20 sheets, add 3 sheets’. Before you start, predict how many rounds it will take and how many sheets there will be at the end. Check the prediction. Then change the start to 8 sheets and predict again. Last, write a loop that never stops, and say what is missing. If you have a computer or tablet, build the doubling loop in a micro:bit simulator.
Practice
1
What does the board show?
The loop runs 5 times and adds 4 each time: 5 × 4 = 20. The board shows 20.
2
How many times does the loop run, and what does the board show?
Round 1: 100 > 40, so n = 75. Round 2: 75 > 40, so n = 50. Round 3: 50 > 40, so n = 25. Then 25 > 40 is false and the loop stops. It runs 3 times and the board shows 25.
3
Is each of these a count-controlled loop or a condition-controlled loop? (a) Play a tune 3 times. (b) Keep adding water while the jug is not full. (c) Press a key 8 times.
(a) count-controlled, because the number of times is set. (b) condition-controlled, because it stops when the jug is full, not after a set number. (c) count-controlled.
4
This program never stops. Explain why and show how to fix it so that it shows 1, 2, 3 and 4.
The value of count is 1 and nothing in the loop changes it, so count < 5 is always true. Fix it by adding a block change count by 1 inside the loop, after the show block. Then count goes 1, 2, 3, 4, and when it reaches 5 the condition is false and the loop stops.