The IF command provides conditional execution of the remaining commands on the line. When IF has an argument, it updates $TEST with the truth value of its evaluated argument. GT.M executes the remainder of a line after an IF statement when $TEST is 1 (TRUE). When $TEST is 0 (FALSE), GT.M does not execute the rest of the line. When the IF argument evaluates to a literal FALSE (0), GT.M discards the command and its arguments at compile time, which means it does not perform any validity checking on the remainder of the line.
The format of the IF command is:
I[F] [tvexpr[,...]]
Because IF is a conditional command, it does not support a command postconditional.
The scope of the IF is the remainder of the line. The scope of an IF can be extended with DO (or XECUTE) commands.
The action of IF is controlled by the value of the expression and by $TEST, if there is no expression.
IF with no argument acts on the existing value of $TEST (which it does not change); in this case, at least two (2) spaces must follow the IF to separate it from the next command on the line.
An indirection operator, and an expression atom evaluating to a list of one or more IF arguments form a legal argument to IF.
Note | |
---|---|
Commands with timeouts also maintain $TEST. For information about $TEST, refer to Chapter 8: “Intrinsic Special Variables”. Because GT.M stacks $TEST only at the execution of an extrinsic or an argumentless DO command, any XECUTE or DO with an argument has the potential side effect of altering $TEST. Use the argumentless IF with caution. |
Example:
IF A,B ... is equivalent to IF A IF B
An IF with more than one argument behaves as if those arguments were logically "ANDed." However, execution of the line ceases with the evaluation of the first false argument. For IF argument expressions containing the "AND" operator (&), by default, execution still ceases with the evaluation of the first false argument, however any global references within the expression act in sequence to maintain the naked reference. The "FULL_BOOLEAN" and "SIDE_EFFECTS" compiler settings modify this behavior if you desire GT.M to provide side effects it would otherwise bypass due to short-circuiting of Boolean expressions.
Postconditionals perform a function similar to IF; however, their scope is limited to a single command or argument, and they do not modify $TEST. For more information on postconditionals, see Chapter 5: “General Language Features of M”.
Example:
IF x=+x!(x="") Do BAL
In this example, the DO executes if x contains a number or a null string.
Example:
Write !,?50,BAL If 'BAL Write "****" IF Set EMPTY(acct)=""
The IF in the first line changes the value of $TEST, determining the execution of the code following the argumentless IF in the second line. Such argumentless IFs may serve as a form of line continuation.
Example:
GTM>Set X=1,Y=1,Z=2 Kill UNDEF GTM>If X=1,Y=1,Z=3,UNDEF=0 Write "HI" GTM>
The IF command causes GT.M to cease executing the line after it determines Z is not equal to three (3). Therefore, GT.M never evaluates the reference to the undefined variable and never generates an error.
Example:
GTM>Set X=1 Kill UNDEF GTM>If X=1!(UNDEF=3) Write "HI" HI GTM>
Because GT.M recognizes that the X=1 fulfills the IF, it skips evaluation of the UNDEF variable and executes this IF command without generating an error. Because GT.M does not require such optimizations and in fact discourages them by requiring that all global references maintain the naked indicator, other implementations may generate an error.