In Forth you can use control structures only inside colon definitions.
if-structure looks like this:
: abs ( n1 -- +n2 ) dup 0 < if negate endif ; 5 abs . -5 abs .
if takes a flag from the stack. If the flag is non-zero (true),
the following code is performed, otherwise execution continues after the
< compares the top two stack
elements and produces a flag:
1 2 < . 2 1 < . 1 1 < .
Actually the standard name for
tutorial presents the examples using
endif, because this is often
less confusing for people familiar with other programming languages
then has a different meaning. If your system does not have
endif, define it with
: endif postpone then ; immediate
You can optionally use an
: min ( n1 n2 -- n ) 2dup < if drop else nip endif ; 2 3 min . 3 2 min .
else-part (hint: what's the definition of