1.4. Using Repeats

Good programmers are lazy in a productive way. They do not like saying the same thing over and over in their code. Doing so tends to make code harder to write, harder to read and harder to change and maintain.

Note

The Golden Rule of Programming:

If you find yourself writing the same code more than one time (using the same squence of blocks), there is almost certainly a better way to do things. You may not know the better way yet, but it is probably out there.

You may have noticed that in the program for the square we drew in the last section there was some repetition: “move, turn, move, turn, …”. The video below introduces how a repeat block can help us eliminate that repitition and in the process create a program that is much easier to modify:

Checkpoint Exercises

Try these exercises to test your understanding and build your programming skills:

../../_images/CP12a.png

Make a script that draws a regular octagon - you should try to draw it with just one Move and one Turn block.

../../_images/CP12a_key.gif
../../_images/CP12b.png

Make a script that draws a hexagon composed of six triangles.

This is much easier to draw if you focus on just drawing 6 triangles, rotating a little between each one. Don’t try to draw the hexagon first.
../../_images/CP12b_key.gif