Star Student Builder logoStar Student Builder
← Block Builders Lesson 2Std 3-5

Loops Save Work

A twelve-sided shape needs twenty-four commands if you type every one. With a loop it needs one line. Programmers love loops, not because they are clever, but because they are lazy in the best way.

πŸ’‘ The idea

A loop repeats work, so one line can do the job of twenty.

  • repeat 6 [ ... ] does the work inside the brackets six times.
  • For a shape with equal sides, the turn is 360 divided by the number of sides.
  • Five sides means turns of 72, because 360 divided by 5 is 72.
  • Eight sides means turns of 45. Twelve sides means turns of 30.
  • The more sides you use, the more the shape looks like a circle.

πŸ“˜ Words to know

loop
a piece of code that repeats
penup
lift the pen so the turtle moves without drawing
polygon
a shape made of straight sides

πŸ‘€ Watch this

A hexagon: six sides, and six turns of 60 that add up to 360.

repeat 6 [ forward 80 right 60 ]
What this program draws
What it draws

✍️ Your turn

  1. Run it and count the sides.
  2. Change 6 and 60 to 3 and 120. You get a triangle.
  3. Work out the turn for a nine-sided shape, then draw it.
  4. Try repeat 36 [ forward 12 right 10 ] and see what appears.
  5. Write down how many lines you saved by using repeat.

🎯 Challenge: Draw a pentagon and an octagon side by side on the same page.

Show me one answer
repeat 5 [ forward 80 right 72 ]
penup
goto -150 0
pendown
repeat 8 [ forward 50 right 45 ]

Your program
Ask GuruAcharya Samayeshwar