Handout 3B

The purpose of this exercise is to become more familiar with the use of the IF and IFELSE commands in LOGO and to introduce the PRINT and OUTPUT commands.

The commands IF and IFELSE are used in a program when you want it to do different things depending on the outcome of some decision. For example, in a tetris game you would want the game to end if a brick reached the top of the screen. Also, most computer programs give a choice of whether to "quit without saving changes" or not when the "exit" option is selected. LOGO handles decisions like this using IF and IFELSE.

The IF command is used when you want the computer to do something exactly when some condition is met. We saw an example of this in the STOPSPIRAL procedure:

IF :SIZE < 0 [STOP]
Here is another example of the use of the IF command:
TO TESTPOS :NUMBER
 IF :NUMBER > 0 [PRINT [THE NUMBER IS POSITIVE.]]
END
This procedure checks whether the argument given to it is positive. If so, it executes the print command enclosed in the brackets after the IF statement. The PRINT command prints the argument given to it -- in this case the argument is a list of words.

It is very common for a program to do one thing under one condition and a different thing if the condition is not met. In UCB LOGO, this is done using an IFELSE statement, which has the format:

IFELSE true/false test [things to do if true] [things to do if false]

Here is a procedure which uses IFELSE to print the absolute value of its argument:

TO PRINTABS :NUMBER
 IFELSE :NUMBER < 0 [PRINT -:NUMBER] [PRINT :NUMBER]
END
As you have observed, the PRINT command prints a number or a list in the LOGO command window. This is a useful way to report the results of a procedure to a human, but not a good way to communicate with other procedures. To make a procedure to return a value to another procedure, use the OUTPUT command. Once LOGO reaches an OUTPUT instruction, it assumes the procedure has completed its task and exits the procedure.

The following example uses OUTPUT to return the absolute value of the argument given. It can be written using IF rather than IFELSE because once an OUTPUT command is executed LOGO exits the procedure.

TO ABS :NUMBER
 IF :NUMBER < 0 [OUTPUT -:NUMBER]
 OUTPUT :NUMBER
END
If you run ABS from the command line, LOGO will tell you that does not know what to do with the output. This is because ABS is a tye of LOGO command called a reporter; it reports or outputs a value when it finishes running. To get rid of the error message, use the output of the ABS command as input to the PRINT command: run PRINT ABS -7 instead of ABS -7.

Write a procedure that takes two numbers as arguments and prints or outputs the maximum of those two numbers.


The procedures XCOR and YCOR output the X and Y coordinates of the turtle on the screen; the turtle starts at position (0,0). Try running the commands PRINT XCOR and PRINT YCOR (XCOR and YCOR are also reporters.) Then move the turtle and try running the commands again. Do this until you understand how XCOR and YCOR work.

The procedure ABS ouputs or reports the absolute value of the argument given to it. The procedure defined below uses these together with IF to move the turtle forward, then turn it if it is too close to the edge of the screen.

TO XYBOUNCESTEP :DIST
 FD :DIST                      ; Move the turtle forward
 IF 100 < (ABS XCOR) [LT 90]   ; Turn left if it's far left/right from 0,0
 IF 100 < (ABS YCOR) [LT 90]   ; Turn left if it's far up/down from 0,0
END
Try running XYBOUNCESTEP with the turtle in many different starting positions. Try it with the turtle pointing up, pointing to the side, and pointing at an angle. Try repeating it:

REPEAT 20 [XYBOUNCESTEP 22]

Use the EDIT command to experiment with different turning angles in the XYBOUNCESTEP procedure. For instance, find out what happens if you change the angle or direction of the turn, or make it a function of XCOR, YCOR or :DIST.


Mtht420