Programming for Children
Primary 4 · Science and Elementary Technology
Programming for Children
Lesson 2 of 5
15 min
Let's begin
Habimana wants the turtle to draw a square. He snaps together: forward 100, right 90, forward 100, right 90, forward 100, right 90, forward 100, right 90. It works! "But that's eight blocks," says Umutoni. "I can do it with three."
How? Let's find out.
A square has 4 equal sides and 4 equal turns. So Umutoni uses the repeat block from the Flow palette:
repeat 4 [ forward 100, right 90 ]
The blocks inside the repeat run 4 times. Repeating steps like this is called a loop.
To draw any shape with equal sides, the turtle must turn all the way round (360 degrees) by the end. So each turn is 360 ÷ number of sides.
| Shape | Sides | Turn | Program |
|---|---|---|---|
| Triangle | 3 | 360 ÷ 3 = 120 | repeat 3 [ forward 100, right 120 ] |
| Square | 4 | 360 ÷ 4 = 90 | repeat 4 [ forward 100, right 90 ] |
| Pentagon | 5 | 360 ÷ 5 = 72 | repeat 5 [ forward 80, right 72 ] |
| Hexagon | 6 | 360 ÷ 6 = 60 | repeat 6 [ forward 60, right 60 ] |
A rectangle has two long and two short sides, so repeat 2 [ forward 150, right 90, forward 80, right 90 ].
A circle: repeat 360 [ forward 1, right 1 ], or use the arc block.
Tip
Always turn 360 in the end
Divide 360 by the number of sides and you get the turn. Triangle 120, square 90, hexagon 60.
Try each one first, then tap Show answer to check.
Write a program for a square with sides of 50 steps.
Answer: repeat 4 [ forward 50, right 90 ]
Mugisha wrote repeat 3 [ forward 100, right 60 ] to draw a triangle, but the shape did not close. What is the bug?
Answer: The turn must be 360 ÷ 3 = 120, not 60. Fix: repeat 3 [ forward 100, right 120 ].
An octagon has 8 sides. What is the turn?
Answer: 360 ÷ 8 = 45 degrees.
How can the turtle move to a new place without leaving a line?
Answer: Use pen up, move it, then pen down again.
What you learned today
Sources and credits
The exercises (Try it yourself and the practice questions) are written by Ganzaa. Turtle Blocks is free software from Sugar Labs.
Quick check
Match each shape to the turn the turtle makes at each corner.
Tap one on the left, then its partner on the right.