Forth conditionals use flags to indicate true/false values. A flag can be generated in several ways, based on testing criteria. The flag can then be displayed from the stack with the word ".", or it can be used as input to a conditional control command. Control commands can cause one behavior if a flag is true and another behavior if the flag is false. Thus, execution can be altered based on the result of a test.
A 0 value indicates that the flag value is false. A -1 or any other nonzero number indicates that the flag value is true.
Table 4-27 lists commands that perform relational tests, and leave a true or false flag result on the stack.
Table 4-27 Comparison Commands
Command |
Stack Diagram |
Description |
---|---|---|
< |
( n1 n2 -- flag ) |
True if n1 < n2. |
<= |
( n1 n2 -- flag ) |
True if n1 <= n2. |
<> |
( n1 n2 -- flag ) |
True if n1 is not equal to n2. |
= |
( n1 n2 -- flag ) |
True if n1 = n2. |
> |
( n1 n2 -- flag ) |
True if n1 > n2. |
>= |
( n1 n2 -- flag ) |
True if n1 >= n2. |
0< |
( n -- flag ) |
True if n < 0. |
0<= |
( n -- flag ) |
True if n <= 0. |
0<> |
( n -- flag ) |
True if n <> 0. |
0= |
( n -- flag ) |
True if n = 0 (also inverts any flag). |
0> |
( n -- flag ) |
True if n > 0. |
0>= |
( n -- flag ) |
True if n >= 0. |
between |
( n min max -- flag ) |
True if min <= n <= max. |
false |
( -- 0 ) |
The value FALSE, which is 0. |
true |
( -- -1 ) |
The value TRUE, which is -1. |
u< |
( u1 u2 -- flag ) |
True if u1 < u2, unsigned. |
u<= |
( u1 u2 -- flag ) |
True if u1 <= u2, unsigned. |
u> |
( u1 u2 -- flag ) |
True if u1 > u2, unsigned. |
u>= |
( u1 u2 -- flag ) |
True if u1 >= u2, unsigned. |
within |
( n min max -- flag ) |
True if min <= n < max. |
> takes two numbers from the stack, and returns true (-1) on the stack if the first number was greater than the second number, or returns false (0) otherwise. An example follows:
ok 3 6 > . 0 (3 is not greater than 6) ok
0= takes one item from the stack, and returns true if that item was 0 or returns false otherwise. This word inverts any flag to its opposite value.