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

Repeat and shorten

Week 19 · Unit 8: Be an artist · 3 lessons
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 algorithmShort algorithm
startstart
marker downmarker 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)
stopstop
15 steps8 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.
AlgorithmSteps
start, marker down, forward 2, turn right (4 times), stop11
start, marker down, repeat 4 times: forward 2, turn right, stop6
A bigger repeat saves more steps. A square with a repeat has 6 steps. Without a repeat it has 11 steps.
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.
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?
3
Does the shorter algorithm draw something different from the long one?
4
The robot draws this rectangle. Write a short algorithm for it with a repeat. It starts at the top left, facing right.