
Page 125
Conditionals and Loops
Forth supports a standard
IF
statement, though the order is switched around from what you might be used to. The
IF
word comes in two different varieties:
condition IF body THEN
The
IF
word looks at the
condition
on the stack. If the condition is true, the body between
IF
and
THEN
is executed. Table 6-3 lists some of the mathematical condition words that are
available.
Table 6-3. Mathematical Condition Words
Word
Description
<
Compares the top two items on the stack; replaces them with true if the second item is less than
the top item, false otherwise
=
Compares the top two items on the stack; replaces them with true if they are equal, false otherwise
>
Compares the top two items on the stack; replaces them with true if the second item is greater
than the top item, false otherwise
0<
Replaces the top item of the stack with true if it is less than zero; false otherwise
0=
Replaces the top item of the stack with true if it is equal to zero; false otherwise
The following example prints out a message if the top item on the stack is less than 0:
: negative 0< IF ." less than zero " THEN ;
ok
-2 negative
less than zero ok
2 negative
ok
The
0<
word examines the top value on the stack. The result of this comparison is examined by the
IF
word. If it is true, the body of the
IF
is executed. The
.“
word tells pbFORTH to print text
to the console, up until the
”
word. Finally,
THEN
marks the end of the body of the
IF
.
There's also a slightly more complicated
IF
word:
condition IF trueBody ELSE falseBody THEN
This is the same as before, except words between
ELSE
and
THEN
will be executed if the condition is false.
Forth also includes some simple loops, including:
limit start DO words LOOP
This loop performs the given words
limit - start
times. Internally, a loop index is given the value
start
. Each time through the loop, the index is increased by one. When it is equal to
limit
, the loop ends. You can put the
Summary of Contents for MINDSTORMS Robots
Page 22: ...Page 18 The back tread wheels are anchored to the shafts with the 16t gears ...
Page 23: ...Page 19 Page 20 Next start building support for the drive motors ...
Page 26: ...Page 23 The touch sensors are attached only by the shaft that runs through them ...
Page 41: ...Page 41 ...
Page 43: ...Next build the support for the light sensor ...
Page 80: ...Page 85 Make sure the bump on the long pin is up against the 4u beam ...
Page 82: ......
Page 84: ...Page 89 ...
Page 85: ...Step 14 is similar to Step 11 take a deep breath and go slowly ...
Page 87: ...Page 91 ...
Page 88: ...Page 92 Make sure that the two sides are at the same angle They should mirror each other ...
Page 89: ...Page 93 Page 94 ...
Page 90: ...Structural Support Page 95 ...
Page 91: ...Idler Wheel Page 96 ...
Page 92: ...Page 97 Drive Motor ...
Page 93: ...While you re putting the motor in hold on to the worm gear so it doesn t slip off Page 98 ...
Page 94: ...Grabber Arm Motor ...
Page 95: ...Page 99 ...
Page 96: ...Page 100 RCX Attach the RCX on both sides as shown Page 101 ...
Page 158: ......
Page 159: ...Page 175 The 16t gears are nestled inside the tread wheels ...
Page 160: ...Page 176 ...
Page 161: ...Page 177 Attach the motors to output A and output C as shown Page 178 ...