<< Click to Display Table of Contents >> Navigation: Reference > Commands > IF |
There are two forms of command construction using IF. The first is the simple IF construction, of the form:
IF logical expression, command,
Here, if the logical expression is true, the command following is executed; if it is false, control passes to the command after next. For more information on logical expressions, see the separate chapter.
The command following IF can be any command except a UL command, a position command (beginning @) or another IF command.
To execute more than one command under IF control, a block IF construction is used. The form is:
IF logical expression, THEN,
command ,
...
ENDIF,
If the logical expression is true, the commands are executed; otherwise, execution continues with the command after ENDIF.
It is possible to specify an alternative set of commands to be executed if logical expression is false, using ELSE:
IF logical expression, THEN,
command,
...
ELSE,
command,
...
ENDIF,
Here, the first set of commands is executed if the logical expression is true and the second set if the logical expression is false.
Finally, it is possible to specify several sets of commands, the execution of each set to be dependent on different logical expressions with ELSIF, as follows:
IF logical expression, THEN,
command,
...
ELSEIF logical expression, THEN,
command,
...
ELSEIF logical expression, THEN,
command,
...
ELSE,
command,
...
ENDIF,
Here, only one of the sets will be executed - the set following the first logical expression found true. Typically, the last ELSE in such a construction will be unconditional, to catch all records not processed in the block so far.
Note that IF and UL may be nested to a depth of 16.
IMPORTANT: the commas after THEN and ELSE must be present.
Examples of simple IF commands:
if $age/1, go to @20,
if $sex/n3, ds $ac = $123/1-5,
Examples of block IF commands:
if $436/5, then,
el 'Illegal record', go to finish,
endif,
!
if $sex/1, then,
ds $ac = $123/1-5,
else,
ds $ac = $124/1-5,
endif,