Repeating things

Page 4

Imagine that you want the turtle to draw a dashed line. Like this:

Repeated line

One way to do that is to first make him turn RT 90. Then repeat this 10 times:

PD
FD 20
PU
FD 20

Why? Because the same thing is repeated many times. First there's a line. Then there's a blank. Then there's another line. And so on.

But writing the same thing many times is not very fun. Let's learn a better way! First we write:

REPEAT

Then we write how many times we want to repeat. If it's 20 times we write 20:

REPEAT 20

Now we write a [ sign:

REPEAT 20 [

Then we write the Logo code we want to repeat. (Sometimes I call things the turtle understands "Logo code" or just "code".) Write it all on the same line. Like this:

REPEAT 20 [PD FD 20 PU FD 20

At the end we write a ]:

REPEAT 20 [PD FD 20 PU FD 20]

That will make the turtle draw a dashed line. [ ] is like a box. Inside it you can put anything you want the turtle to repeat.

REPEAT 10 [           ]

If you put something in that box it will be repeated 10 times. You can try it now.

You can repeat anything with REPEAT. Maybe you want to make many houses and boats instead of just one? Then you can use REPEAT.

Here's a staircase I drew with REPEAT:

Red staircase

This is what I told the turtle:

REPEAT 7 [FD 30 RT 90 FD 30 LT 90]

Can you draw a staircase with more steps than seven? With fewer steps? Can you draw a staircase that goes in the other direction? Can you draw one with smaller steps? You don't have to if you don't want to. You can draw something you like better too.

What happens if you tell the turtle this?:

REPEAT 4 [FD 100 RT 90]

Think Turtle before you run it. Pretend you are the turtle and walk like he would walk. Then write in your Logo program to see if you were right.

These are the Logo words we know now. REPEAT is new:

Logo word What it means
FD 100 Walk forward 100 steps
BK 100 Walk backward 100 steps
LT 90 Turn left (good for drawing squares)
RT 90 Turn right (good for drawing squares)
LT 120 Turn left (good for drawing triangles)
RT 120 Turn right (good for drawing triangles)
CS Remove everything you have drawn
HOME Move back to the starting point (and draw a line)
PU Lift the pen so you won't draw when you walk
PD Put the pen towards the screen again
REPEAT 10 [       ] Repeat what's in the box 10 times

Turn page, or back to the table of contents.