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.
Step
Instruction
1
Start
2
Pick up a bead
3
Thread it on the string
4
Repeat steps 2 and 3 four more times
5
Stop
Worked examples
Long
Short
Steps
12
5
Beads on the string
5
5
Steps saved
7
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.
Step
Instruction
1
Start
2
Put on gardening gloves
3
Dig a hole
4
Plant a seed
5
Repeat steps 3 and 4 three more times
6
Take off the gloves
7
Stop
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.
Action
Long
Short
stamp
3
3
clap
3
3
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?