In Forth you can use control structures only inside colon definitions.
An if
-structure looks like this:
if
takes a flag from the stack. If the flag is non-zero (true),
the following code is performed, otherwise execution continues after the
endif
(or else
). <
compares the top two stack
elements and produces a flag:
Actually the standard name for endif
is then
. This
tutorial presents the examples using endif
, because this is often
less confusing for people familiar with other programming languages
where then
has a different meaning. If your system does not have
endif
, define it with
You can optionally use an else
-part:
Assignment: Write
min
withoutelse
-part (hint: what’s the definition ofnip
?).
Reference: Selection.