AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Choices and loops on the board

Week 19 · Unit 8: Be an innovator · 3 lessons
2

Do it again: loops

Computing 6: Unit 8, pp. 106–123
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.

settotalto0changetotalby3repeat4timesshow numbertotalon buttonApressed
Worked examples
Roundtotal beforetotal after
103
236
369
4912
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.
setnto1setnton×2whilen<50show numbernon buttonApressed
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.
Roundn beforen < 50?n after
11true2
22true4
34true8
48true16
516true32
632true64
check64falsestop
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?
setcoinsto0changecoinsby4repeat5timesshow numbercoinson buttonApressed
2
How many times does the loop run, and what does the board show?
setnto100changenby-25whilen>40show numbernon buttonApressed
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.
4
This program never stops. Explain why and show how to fix it so that it shows 1, 2, 3 and 4.
setcountto1show numbercountwhilecount<5on buttonApressed