flag IF code THEN
If flag is non-zero (as far as IF
etc. are concerned, a
non-zero cell represents truth), code is executed.
You may wonder why then
ends an if
construct, which is
at odds with the usage of then
in some other programming
languages, and with the idiom “if ... then ...” in English.
According to Webster’s New Encyclopedic Dictionary, then
(adv.) has the following meanings:
... 2b: following next after in order ... 3d: as a necessary consequence (if you were there, then you saw them).
Forth’s then
has the meaning 2b, whereas THEN
in Pascal
and many other programming languages has the meaning 3d. If you do
not like this usage of then
, Gforth (but not Standard Forth)
also has endif
, which can be used in its place. Adding
ENDIF
to a system that only supplies THEN
is simple:
: ENDIF POSTPONE then ; immediate
flag IF code1 ELSE code2 THEN
If flag is true, code1 is executed, otherwise code2 is executed.
Gforth also provides the words ?DUP-IF
and ?DUP-0=-IF
, so
you can avoid using ?dup
. Using these alternatives is also more
efficient than using ?dup
. Definitions in Standard Forth
for ENDIF
, ?DUP-IF
and ?DUP-0=-IF
are provided in
compat/control.fs.
x CASE x1 OF code1 ENDOF x2 OF code2 ENDOF ... ( x ) default-code ( x ) ENDCASE ( )
Executes the first codei, where the xi is equal to x. If no
xi matches, the optional default-code is executed. The optional
default case can be added by simply writing the code after the last
ENDOF
. It may use x, which is on top of the stack, but must
not consume it. The value x is consumed by this construction
(either by an OF
that matches, or by the ENDCASE
, if no OF
matches). Example:
: num-name ( n -- c-addr u ) case 0 of s" zero " endof 1 of s" one " endof 2 of s" two " endof \ default case: s" other number" rot \ get n on top so ENDCASE can drop it endcase ;
Programming style note:
To keep the code understandable, you should ensure that you change the stack in the same way (wrt. number and types of stack items consumed and pushed) on all paths through a selection structure.