Counting repetitions

Page 15

Try this code:

REPEAT 10 [PRINT REPCOUNT]

The turtle should write the numbers from 1 to 10 on a line each. Why does he do that? Can you guess?

REPCOUNT is the number of the current repetition. That means that REPCOUNT is 1 the first time the turtle does the thing in the repetition box. The second time REPCOUNT is 2. The third time REPCOUNT is 3. And so on.

What do you think happens now?:

REPEAT 10 [PRINT REPCOUNT + 5]

Were you right? How would you do to print 2 4 6 8 10 12 14 16 18 20?

We can use REPCOUNT to draw nice things. Here are some colored boxes I drew with it:

Colored squares with REPCOUNT

This is how I did it:

REPEAT 10 [SETPC REPCOUNT SQUARE REPCOUNT*10]

In a repetition box you can always use REPCOUNT. Here is another example. This draws a spiral:

REPEAT 30 [FD REPCOUNT*5 RT 90]

Try it! That code is not so easy to understand. It might seem like a mystery that it draws a spiral. But that's ok. Try to Think Turtle. Then it will be easier to see how it works.

Change the code so that the turtle turns RT 50 instead of RT 90. What happens? Can you draw a spiral that looks like this?:

A spiral

(Hint: you must take very small steps! Size 10 is too big. Maybe size 1 is too big too?)

Try to draw some more things with REPCOUNT. You can use it in many ways. Don't be afraid to experiment. If something doesn't work just try again.

These are the words we know now. REPCOUNT 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
SETPC 4 Change the pen to pen number 4
TO ... END Remember the word after TO in the future
; Don't listen to me now
PRINT [I'm a turtle!] Say "I'm a turtle!"
RANDOM 10 Choose a number from 0 to 9
WAIT 60 Wait for 1 second.
PRINT 2+2 Makes the turtle say 4
TO SQUARE :top Put 5 in your :top pocket when I say SQUARE 5
REPCOUNT The number of the repetition

These are the words we have taught the turtle. They are the same as on the last page.

New Logo word What it means
SQUARE :top Draw a square
CIRCLE Draw a circle
STAR :number :length Draw a star

Turn page, or back to the table of contents.