2
Count the steps
Computing 2: Unit 8, pp. 106–121
GoalCount the steps in an algorithm and see that a repeat makes it shorter.
New words
count · to find how manyround · one run of the steps inside a repeattrace · to follow the steps one by one to see what they do
Explanation
Two algorithms can draw the same picture. We count the lines of each one. The algorithm with fewer lines is shorter. A repeat line counts as one step. Each step inside it counts once, however many rounds there are. Always check by tracing that the short algorithm draws the same picture as the long one.
| Long algorithm | Short algorithm |
|---|---|
| start | start |
| marker down | marker down |
| forward 1, turn left, forward 1, turn right (3 times, 12 lines) | repeat 3 times: forward 1, turn left, forward 1, turn right (5 lines) |
| stop | stop |
| 15 steps | 8 steps |
Worked examples
The stairs. The robot starts at the bottom left, facing right. Each round it goes forward 1, turns left, goes forward 1 and turns right. After 3 rounds it has climbed 3 steps.
Count the short algorithm: start (1), marker down (2), repeat 3 times (3), forward 1 (4), turn left (5), forward 1 (6), turn right (7), stop (8). So it has 8 steps. The long one has 15.
| Algorithm | Steps |
|---|---|
| start, marker down, forward 2, turn right (4 times), stop | 11 |
| start, marker down, repeat 4 times: forward 2, turn right, stop | 6 |
Class activity
Step counting game: write the long algorithm for a square on cards, one step on each card: start, marker down, then forward 2 and turn right four times, then stop. Count the cards. Now make the short algorithm with a repeat card. Count again. How many fewer steps? Ask a partner to follow each one on the floor. Do they walk the same square?
Practice
1
Count the steps: start, marker down, repeat 2 times: forward 3, turn left, stop.
6 steps: start, marker down, repeat, forward 3, turn left, stop.
2
Algorithm A: start, marker down, forward 1, forward 1, forward 1, forward 1, forward 1, forward 1, stop. Algorithm B: start, marker down, repeat 6 times: forward 1, stop. Both draw the same line. Which is shorter and by how many steps?
A has 9 steps and B has 5 steps. B is shorter by 4 steps.
3
Does the shorter algorithm draw something different from the long one?
No. It draws the same picture.
4
The robot draws this rectangle. Write a short algorithm for it with a repeat. It starts at the top left, facing right.
Start, marker down, repeat 2 times: forward 4, turn right, forward 2, turn right, stop.