Next: , Previous: Local Variables Tutorial, Up: Tutorial


3.16 Conditional execution

In Forth you can use control structures only inside colon definitions. An 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 endif (or else). < compares the top two stack elements and produces a flag:

     1 2 < .
     2 1 < .
     1 1 < .

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

     : endif postpone then ; immediate

You can optionally use an else-part:

     : min ( n1 n2 -- n )
       2dup < if
         drop
       else
         nip
       endif ;
     2 3 min .
     3 2 min .
Assignment: Write min without else-part (hint: what's the definition of nip?).

Reference: Selection.