Looping Behaviors

Computers love to repeat things again and again. Humans? Not so much. By now you have made some polygons by dragging and snapping multiple blocks together.

Using Loops

Good programmers are LAZY! They want to make the shortest code possible so they don't have to add so many blocks. Imagine if you made a mistake (a bug) in the code and you had to read all of it to find it. Or what if you wanted to change the size of a shape? You would have to change all the forward() blocks to make it work.

  • Look at your polygon code blocks, do you see a pattern?

  • How many times do the blocks repeat?

  • Click on the Flow palette to the left and select a repeat() block.

  • Now, update your polygon code to use the repeat() block.

Finite Loops

Finite loops repeat for a set number of times. Like making a square, the loop only repeats 4 times to make only four sides and four angles.

Infinite Loops

These are loops that repeat until something else happens. Basically, they repeat endlessly, or forever, until a certain condition is met.

"Forever is cheap"

Lots of new coders like to use the forever() loop to make their Turtle Art creations. It's fun to see the turtle move and paint on the screen. Try not to use the forever() block for making polygons and shapes.

It is better use a finite repeat() block because the computer will repeat the shape endlessly and slow down your program. Good code doesn't do more than it should.