Handout 4B

The purpose of this exercise is to become more familiar with the use of the logical operations NOT, AND and OR and to review the IFELSE command.

Recall the XBOUNCESTEP procedure. You will probably need to redefine both the XYBOUNCESTEP and the ABS procedure:

TO ABS :NUMBER
 IF :NUMBER < 0 [OUTPUT -:NUMBER]
 OUTPUT :NUMBER
END

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
This procedure moves the turtle forward, then checks to see where it is using XCOR and YCOR. If the turtle is close to the edge of the screen, it turns either 90 or 180 degrees to the left.

Compare this to the following procedure:

TO BOUNCE2 :DIST
 FD :DIST
 IF (OR (100 < (ABS XCOR)) (100 < (ABS YCOR))) [LT 90]
END
The logical operator OR is used by LOGO to combine the results of two true/false tests. It is entered into the computer as follows. The output of the OR operator is itself the result of a true/false test.

(OR (true/false test) (true/false test))

As you would expect, if either of the two true/false tests is true the output of the OR operator is also true. So BOUNCE2 uses XCOR and YCOR to check to see if the turtle is close to the edge of the screen and turns the turtle 90 degrees to the left if it is. BOUNCE2 will never turn the turtle 180 degrees, while XYBOUNCESTEP sometimes will. (When does XYBOUNCESTEP turn the turtle a total of 180 degrees?)

Not surprisingly, there is also an AND operator that returns true only when the results of the two true/false tests given as arguments are both true:

(AND (true/false test) (true/false test))

What happens if you replace the OR operator by AND, as in the following example?

TO BOUNCE3 :DIST
 FD :DIST
 IF (AND (100 < (ABS XCOR)) (100 < (ABS YCOR))) [LT 90]
END
Experiment with the BOUNCE3 command. Be sure to try a few different examples in which the turtle has been rotated through some angle. Can you describe how and why the turtle's behavior here is different from that caused by BOUNCE2?


The third logical operator in LOGO is NOT. This command just reverses the output of a true/false test. For example, you could use NOT to test whether a number is less than or equal to zero, as in the following example:

TO STOPSPIRAL2 :SIZE
 IF (NOT (:SIZE > 0)) [STOP]
 FORWARD :SIZE
 RIGHT 90
 STOPSPIRAL2 :SIZE - 10
END
Notice that the turtle finishes drawing the last line and turns exactly 90 degrees to the right. In the previous version of STOPSPIRAL it finished by moving forward 0 and turning right 90, and so pointed back along the last line drawn.

The syntax for the NOT operator is:

(NOT (true/false test))

If you have time, use NOT, AND and OR to draw some spirals or "bounce" designs.


Mtht420