Top: Forth Index Prev: Arithmetic Next: Doing It Again & Again

True or False?

Conditional statements react based upon whether the top stack item is true or false. Most of the time, you are comparing two stack items, therefore you need operators to handle comparisons. Here are a few of them. In all cases, non-zero is true and zero is false. All of the comparison operators return -1 for true.

= ( n1 n2 -- flag ) Is n1 equal to n2?

< ( n1 n2 -- flag ) Is n1 less than n2?

<= ( n1 n2 -- flag ) Is n1 less than or equal to n2?

> ( n1 n2 -- flag ) Is n1 greater than n2?

>= ( n1 n2 -- flag ) Is n1 greater than or equal to n2?

0= ( n1 -- flag ) Is n1 equal to zero?

0< ( n1 -- flag ) Is n1 less than zero?

0> ( n1 -- flag ) Is n1 greater than zero?


IF-THEN And IF-ELSE-THEN

The IF-THEN statement either does something or lets the program keep on moving. For example, "If the sun is shining, I will go outside and read a book, otherwise, I will stay in and read."

    : ?sun  ( f -- )    \ Go outside if the sun is out.
        if
            go-outside
        then
        read-book  ;
So if the flag f is non-zero, the sun is out and I will head outside and then read a book. If the flag is zero, or false, the sun is unable to penetrate the cloud cover and I just read a book indoors.

The IF-ELSE-THEN statement gives you an alternate choice if the IF statement is false. For example, "If the sun is shining, I will go outside and read a book, otherwise, I will wash the dishes and then read."

    : ?sun  ( f -- )    \ Go outside if the sun is out.
        if
            go-outside
        else
            wash-dishes
        then
        read-book  ;
You can also nest IF statements to your heart's content:

    if
        if
            if
                if
                else
                then
            then
        else
        then
    then
If you need to stop execution because of an invalid response, bad data or hardware failure, you can use abort and abort" to stop the program and clear the stack.

    : (checkrunmode)    ( -- )  \ Abort with message if in CRUN mode.
                                \ Some variables must NOT change while
                                \ in CRUN mode words that change them
                                \ call checkrunmode first.
        \runflag @
        if   
            cr bell ." Please stop CRUN mode before changing this variable."
            abort  
        then ;
This word will stop the program and clear the stack if you try to change a certain detector parameters while in CRUN mode (BURST mode is the usual run mode). Another way to write this word, sans bell and carriage return, is:
    : (checkrunmode)    ( -- )  \ Abort with message if in CRUN mode.
                                \ Some variables must NOT change while
                                \ in CRUN mode words that change them
                                \ call checkrunmode first.
        \runflag @
        abort" Please stop CRUN mode before changing this variable."  ;
The difference is that abort" will display the message following it only if \runflag is true. You can get the same functionality as the first (checkrunmode) if you redefine abort" before using it:
    : abort"    ( flag -- )
        cr bell abort"  ;
This works because FORTH will call the previous definition of abort", not the one you are currently defining. Any word defined after our new version of abort" will use the new version and not the old.
Top: Forth Index Prev: Arithmetic Next: Doing It Again & Again