Making Choices -- Conditional Execution
What things can Karel find out about in his world?
Karel can evaluate test functions that tell him whether something
about his world is true or false.
- Karel can test things about walls aroud him (using his cameras):
-
front-is-clear
-
front-is-blocked
-
right-is-clear
-
right-is-blocked
-
left-is-clear
-
left-is-blocked
- Karel can test things about beepers:
-
next-to-a-beeper
-
not-next-to-a-beeper
-
any-beepers-in-bag
-
no-beepers-in-bag
- Karel can test things about the direction he's facing:
-
facing-north
-
not-facing-north
-
facing-south
-
not-facing-south
-
facing-east
-
not-facing-east
-
facing-west
-
not-facing-west
How can Karel make choices?
Karel can decide to do one thing or another based on whether a test is
true or false. For example to make sure Karel doesn't try to pick up
a beeper when none is available:
BEGINNING-OF-PROGRAM
BEGINNING-OF-EXECUTION
IF next-to-a-beeper
THEN
pickbeeper
ELSE
move
END
turnoff
END-OF-EXECUTION
END-OF-PROGRAM
The IF
is followed by a test function. The THEN
and ELSE
are followed by instructions.
The END
shows where this instruction stops.
You can leave out the ELSE part if it's not necessary or place no
instructions between THEN
and ELSE
or
ELSE
and END
.
This document is copyright 1994 by Joseph N. Wilson.
$Id$