= ( 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?
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 thenIf 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.