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

Drawing shapes with loops

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

Fix and shorten loops

Computing 4: Unit 4, pp. 48–61
GoalShorten a long drawing program with a repeat block and find bugs in a shape loop.
New words
bug · a mistake in a programdebug · to find and fix the mistakes in a programtest · to run a program and see if it does the right job
Explanation

A long drawing program often has the same two blocks, move and turn, written again and again. We can shorten it with a repeat block. Count how many times the pair is written and put that number in the repeat block. The short program must draw exactly the same picture. If a shape comes out wrong, there is a bug. To debug, check three things. Is the repeat number the same as the number of sides? Is the turn angle 360 divided by the number of sides? Is the pen down before the loop starts? Fix one bug at a time and test the program again each time.

BugWhat you seeFix
repeat number too smallthe shape is openuse the number of sides
wrong turn anglethe wrong shape, or lines drawn over each otheruse 360 ÷ sides
pen down missing or too latenothing is drawnput pen down before the loop
Worked examples
Longwhenclickedpen downmove70stepsturn ↻90degreesmove70stepsturn ↻90degreesmove70stepsturn ↻90degreesmove70stepsturn ↻90degreesShortwhenclickedpen downmove70stepsturn ↻90degreesrepeat4
This long script draws a shape with 4 equal sides by writing the move and the turn four times. The short script does the same job with a repeat block. The long one has 8 blocks for the shape and the short one has 3, so 5 blocks are saved.
whenclickedpen downmove40stepsturn ↻72degreesrepeat4
Here the sprite should draw a pentagon, which needs 5 sides and a turn of 72 degrees. The repeat number is 4, so the last side is missing and the shape stays open. The fix is repeat (5).
whenclickedpen downmove40stepsturn ↻90degreesrepeat6
This script should draw a hexagon, but the turn is 90 instead of 60. The sprite turns 6 × 90 = 540 degrees, which is more than one full turn. It draws a four-sided shape and then goes over two of the sides again.
Class activity

Bug hunt (with a friend): each of you writes the algorithm for a regular shape on paper, such as “repeat 5: move 4 cm, turn right 72 degrees”, but hides one bug, for example a repeat number that is too small. Swap papers. Draw the shape with a ruler, one step at a time, turning by eye (or with a protractor if you have one). Does it close? Find the bug and write the fixed version.

Practice
1
The steps “move (30) steps” and “turn ↻ (120) degrees” are written three times. Write a shorter version.
2
This script should draw a hexagon but stays open. Which block is the bug and what is the fix?
whenclickedpen downmove20stepsturn ↻60degreesrepeat5
3
This script draws nothing. Why, and how do you fix it?
whenclickedmove50stepsturn ↻90degreesrepeat4pen down
4
A hexagon drawn without a loop needs 12 blocks, six moves and six turns. How many blocks does the version with a repeat block need, and how many are saved?