IF ... THEN ... END
If an ELSE clause isn't required-that is, if the choices are to do
something or do nothing-you can omit ELSE from the program
structure. The following example ensures that the object in level
greater than the object in level 2 by swapping them if necessary.
IF DUP2 , THEN SWAP END
Note the use of DUP2 to make copies of the objects. The copies are
then consumed by the comparison ,. For another example of IF
THEN ... END, see "SORT (Sort a List)" on page 270.
1FT (If- Then-End Command)
You could write the previous example by using the command 1FT in-
stead of the program structure:
D U P 2
'leaves a flag on the stack, the program
goes on the stack, and the command
takes the flag
and the program as arguments. If the flag is true, 1FT evaluates the
program; if the flag is false, 1FT drops the program. The result is iden-
tical to the program-structure form.
In some cases you can predict that an error might occur during pro-
gram execution. Normally an error cancels program execution; but if
the offending command by enclosing it in a special program
structure, the program can continue execution when the error occurs.
Remember the problem with (sin x)/x-it causes an Infinite Result er-
ror when x
O. Another method for defining (sin 0)/0
IFERR X SIN X / THEN DROP2 1 END
26: Program Structures