Efficient Programs
Drawing a Square
Take a look at this program that draws a square.
Notice that when drawing the square it uses the same instructions again and again as it draws each side:
Good programs try and use as little code as possible.
One way of doing this is looking for repeated instructions and putting them inside a loop.
When we know how many times we want the code to repeat (n), we can use the following block in scratch:
So we could improve our square program by repeating the instruction to draw a side 4 times:
Watch the video below to see how to use the repeat loop to draw a square and a pentagon.
In the video, we created a program that uses the repeat n loop to draw a pentagon. As it did so, the sprite went on a little journey back to where it started, but ended up pointing the same way. In total, it rotated a full 360°.
When drawing the pentagon, the sprite turned a full 360°.
It took five turns of 72° to do it.
72° X 5 = 360°
If we were to draw a Hexagon it would take 6 turns to make the journey of 360°, so each turn would be 360° ÷ 6 = 60.
If we were to draw a Heptagon it would take 7 turns to make the journey of 360°.
Challenges
Create programs that will draw:
A hexagon (6 sides)
A heptagon (7 sides)
An Octagon (8 sides)
Key Words
Loop
A programming structure that is able to repeat the code inside it.
How to Download the files and use them in Scratch....
...on a computer.
...on a smartphone or tablet