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

Counting loops

Week 8 · Unit 4: Be a storyteller · 3 lessons
3

Shorter with a loop

Computing 4: Unit 4, pp. 48–61
GoalShorten a long algorithm with a count-controlled loop and count the steps saved.
New words
shorten · to make shorter without changing what the job doesrepeat step · a step that tells you to go back and do some steps againsteps saved · how many fewer steps the short algorithm has
Explanation

A long algorithm often has the same steps written out again and again. This takes time, and mistakes creep in. We can shorten it with a count-controlled loop. Write the steps that repeat once, then add a step that says how many more times to repeat them. The short algorithm must do exactly the same job as the long one. To check, count how many times each action happens in both. Then count the steps to see how many were saved. For a necklace of 5 beads, the long algorithm has 12 steps and the short one has 5.

StepInstruction
1Start
2Pick up a bead
3Thread it on the string
4Repeat steps 2 and 3 four more times
5Stop
Worked examples
LongShort
Steps125
Beads on the string55
Steps saved7
The long necklace algorithm writes “Pick up a bead” and “Thread it” five times, plus Start and Stop. That is 1 + 10 + 1 = 12 steps. The short one has 5 steps. Both put 5 beads on the string.
StepInstruction
1Start
2Put on gardening gloves
3Dig a hole
4Plant a seed
5Repeat steps 3 and 4 three more times
6Take off the gloves
7Stop
Steps that happen only once stay outside the loop. Here the gloves go on once, then steps 3 and 4 repeat, then the gloves come off once. Four seeds are planted. If the repeat said “steps 2 to 4”, the gloves would go on four times, which makes no sense.
ActionLongShort
stamp33
clap33
To check a short algorithm, count the actions. The long one says “stamp, clap” three times. The short one says “stamp, clap” once, then “repeat two more times”. Both give 3 stamps and 3 claps.
Class activity

Shorten it (with a friend): write each step of this long algorithm on its own paper strip: Start, Pick up a bead, Thread it, Pick up a bead, Thread it, Pick up a bead, Thread it, Stop. Lay the strips out and find the pair that repeats. Take away the extra strips and add one strip that says “Repeat steps 2 and 3 two more times”. Count the strips before and after. Read your short algorithm to your friend. Do you both get 3 beads?

Practice
1
Shorten this algorithm: 1 Start 2 Stamp 3 Clap 4 Stamp 5 Clap 6 Stamp 7 Clap 8 Stop.
StepInstruction
1Start
2Stamp
3Clap
4Stamp
5Clap
6Stamp
7Clap
8Stop
2
How many steps did you save in the last question?
3
Which steps are done only once, and how many cards are sorted?
StepInstruction
1Start
2Open the box
3Take out a card
4Sort the card
5Repeat steps 3 and 4 six more times
6Stop
4
A job needs steps 2 and 3 done 9 times in all. Complete the step: Repeat steps 2 and 3 __ more times.