If this then that

Page 18

Write this code in the editor and try it:

TO BIG :number
  IF (:number > 10) [PRINT [Yes!]]
END

Can you guess what will happen? Then try it. What happens when you try BIG 12? How about BIG 7? BIG 100? Do you see the pattern?

Whenever the turtle sees this:

IF ( ) [ ]

he looks between the ( ). If what is there is true, then he does what's inside [ ].

So if we write BIG 12 this is what happens: first the turtle looks at ( ). There he sees:

:number > 10

He must look in his :number pocket. The number he has there is 12. So he asks himself if 12 > 10. ("12 > 10" means "12 is bigger than 10".) Is 12 > 10? Yes, so he should look in [ ]. There he sees:

PRINT [Yes!]

So he says "Yes!".

If we had tried BIG 7 instead he would have seen 7 > 10. That's not true. (7 is not bigger than 10!). So the turtle doesn't look in the [ ] box.

We can also have another box. Then we should write IFELSE. Like this:

IFELSE ( ) [ ] [ ]

Then the turtle will do what's in the second box only if what was in ( ) was false. We can also have more than one parameter. Maybe like this:

TO BIGGER :first :second
  IFELSE (:first > :second) [PRINT [Yes!]] [PRINT [No!]]
END

IF and IFELSE are very important commands. On the next page they will help us draw things we couldn't draw without them.

These are the words we know now. IF and IFELSE are 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
IF ( ) [ ] Is ( ) true? Then do [ ]
IFELSE ( ) [ ] [ ] Is ( ) true? Then do the first [ ]. Otherwise do the second [ ]

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 :x :size Draw a circle
STAR :number :length Draw a star
LEAF Draw a leaf
BLOOM Draw a bloom

Turn page, or back to the table of contents.