Program Flow
REPEAT IF…THEN…ELSE STOP WAIT OUTPUT TEST IFTRUE IFFALSE
This section shows you how to use commands that control programs.
REPEAT number [list of commands]
REPEAT
allows you to run the same commands multiple times. It not only saves a lot of typing, but also allows you to create very interesting designs.
You can draw regular shapes of different sizes, repeat patterns, and even nest REPEAT
s, putting one inside the other.
For example, here is a square:
REPEAT 4 [FD 100 RT 90]
Can you imagine what this instruction might do before trying it?
REPEAT 8 [REPEAT 8 [FD 50 RT 45] RT 45]
» Things to Try
Experiment with REPEAT
to see what kinds of designs you can create!
IF expression [then-instructions]
IF expression [then-instructions] [else-instructions]
IF expression THEN instructions
IF expression THEN instructions ELSE instructions
IF
runs instructions based on a condition your specify.
Try these (remember that HEADING
reports the direction the turtle is pointing: 180 is facing down, 0 is facing up):
IF HEADING = 180 THEN BK 100
IF HEADING = 0 THEN BK 100 ELSE FD 100
You could also write the instructions this way.
IF HEADING = 180 [BK 100]
IF HEADING = 0 [BK 100] [FD 100]
In this set of instructions, if the turtle’s pen is down, the PENUP
command raises the pen.
IF PENDOWN? [PENUP]
In English, you would says, “If the pen is down, raise the pen.”
The reverse would use the NOT
command. NOT
reports the opposite of the reporter it comes before.
IF NOT PENDOWN? [PENDOWN]
In English, you would says, “If the pen isn’t already down, put the pen down.”
Do you see how that works?
» Things to Try
You can use IF
commands in many ways.
You could create a number guessing game. Suppose the number that the user is supposed to guess is 57.
MAKE "ANSWER 57
PRINT [GUESS A NUMBER BETWEEN 1 AND 100.]
MAKE "GUESS READWORD
IF :GUESS = :ANSWER [PRINT [YOU GUESSED IT!] STOP]
IF :GUESS > :ANSWER [PRINT [THAT NUMBER IS TOO HIGH. TRY AGAIN.]] [PRINT [THAT NUMBER IS TOO LOW. TRY AGAIN.]]
and so on. Do you see how IF
works?
Here is a complete guessing game program. It uses recursion, which is when a procedure calls itself.
After each wrong guess, the GET.GUESS
procedure runs again and collects another guess.
When the correct number is guessed, the GET.GUESS
program stops, which ends the whole program.
If there were other commands to run in NUMBER.GUESS
, those would run after GET.GUESS
stops.
TO NUMBER.GUESS
MAKE "ANSWER 57
GET.GUESS
END
TO GET.GUESS
PRINT [GUESS A NUMBER BETWEEN 1 AND 100.]
MAKE "GUESS READWORD
IF :GUESS = :ANSWER [PRINT [YOU GUESSED IT!] STOP]
IF :GUESS > :ANSWER [PRINT [THAT NUMBER IS TOO HIGH. TRY AGAIN.]] [PRINT [THAT NUMBER IS TOO LOW. TRY AGAIN.]]
GET.GUESS
END
NUMBER.GUESS
STOP
As you saw in the number guessing game above, STOP
causes the current procedure to stop running and return control to the procedure that called it.
Here’s a simple example of one procedure that uses STOP
.
TO MAKE.IT.STOP
FD 20
IF XCOR > 180 THEN STOP
MAKE.IT.STOP
END
DRAW
MAKE.IT.STOP
Here is an example of a procedure that stops and returns control to the procedure that called it.
TO STEPS
DRAW
STAIRCASE
PRINT [DID YOU LIKE MY STAIRCASE?]
END
TO STAIRCASE
FD 20 RT 90 FD 20 LT 90
IF XCOR > 200 THEN STOP
STAIRCASE
END
STEPS
» Things to Try
Write procedures that check a condition and then stop.
WAIT number
WAIT
causes your program to pause for a certain length of time. The input number represents milliseconds.
A millisecond is a thousandth of a second, so WAIT 1000
pauses one second.
» Things to Try
Think of when you might use WAIT
. You saw an example in the Music and Sounds section.
You can insert a WAIT
command between sounds that you play.
You could even insert a WAIT
between drawing commands, like this:
REPEAT 4 [FD 50 WAIT 500 RT 90 WAIT 500]
OUTPUT
OUTPUT
allows you to write a procedure that is a reporter.
For example, you could write a procedure that converts inches to millimeters.
This procedure takes a number of inches as input and outputs the equivalent in millimeters.
TO CONVERT :INCHES
OUTPUT :INCHES * 25.4
END
CONVERT 10
Result: 25.4
CONVERT 57
Result: 1147.8
You can do what you want with the output. You could print it or use it as input to another command or procedure.
PR CONVERT 10
FD CONVERT 100
» Things to Try
Think of other reporters you could write that output information.
TEST condition
IFTRUE [instruction-list]
IFT [instruction-list]
IFFALSE [instruction-list]
IFF [instruction-list]
Although these are three separate commands — TEST
, IFTRUE
, IFFALSE
— we will combine them here into one group because they work together. You can’t use IFTRUE
or IFFALSE
if you haven’t used TEST
. Let’s see how to use them.
TEST
checks a condition you give and stores the result as either TRUE or FALSE. You can then use IFTRUE
and IFFALSE
to act on that information. You can think of these commands as relatives of the IF
command.
TEST HEADING = 0
IFTRUE [SETH 180]
IFFALSE [SETH 0]
What these instructions say are: see if the turtle is heading straight up. If it is, turn it the opposite way. If it is not facing straight up, make it point straight up.
Another way to code this would be to use IF
:
IF HEADING = 0 [SETH 180] [SETH 0]
or IF HEADING = 0 THEN SETH 180 ELSE SETH 0
Why would you choose to use three instructions rather than just one to do the same job?
Because you can first check the condition with TEST
. Then, later on (either in the same procedure or in the procedure that called the current procedure), you can use IFTRUE
and/or IFFALSE
to make some action.
TO HEADING.CHECK
SETHEADING 90 * RANDOM 4
TEST HEADING = 0
REPEAT 4 [FD 100 RT 90]
IFTRUE [PR [YOU USED TO BE HEADING AT 0]]
IFFALSE [PR [YOU USED TO BE HEADING AT A DIFFERENT ANGLE]]
END
DRAW HEADING.CHECK
In this example, the IFTRUE
and IFFALSE
statements are in a subprocedure. The original TEST HEADING = 0
is still remembered.
TO HEADING.CHECK
SETHEADING 90 * RANDOM 4
TEST HEADING = 0
SQ
END
TO SQ
REPEAT 4 [FD 100 RT 90]
IFTRUE [PR [YOU USED TO BE HEADING AT 0]]
IFFALSE [PR [YOU USED TO BE HEADING AT A DIFFERENT ANGLE]]
END
» Things to Try
Try some experiments using both IF
and also TEST
with its companions IFTRUE
and IFFALSE
.