Conditionals

This section is concerned with decision making. How can your program react to values on the stack and decide how to manipulate them?

Most Forths provide a few conditionals and a word named if to handle this. However, in RetroForth, the conditionals are bound to if. RetroForth therefore has special forms of if, which are used directly with a condition. In California, the drinking age for alcohol is 21. You could write a simple word now to help bartenders. Enter:

: DRINK? ( age -- flag ) 20 >if ." OK" cr ;; then ." Underage!" cr ;

20 DRINK?
21 DRINK?
43 DRINK?

Here you are introduced to the if/;;/then structure of Forth conditional statements. The word ;; is used to force an exit to a word. It's useful in both conditional constructs and looping constructs. Other useful "if" constructs are:

<if If second stack item is less than TOS
=if Two top stack items are equal
<>if Top two stack items are not equal
?if Is the conditional flag TRUE?
true Set the conditional flag to TRUE
false Set the conditional flag to FALSE
;; Exit the current word
0; Exit if TOS=0, otherwise continue execution