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.

The format of the IF command is:

I[F] [tvexpr[,...]]
[Note] 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.


IF A,B ...
is equivalent to

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 (&), execution still ceases with the evaluation of the first false argument. Any global references within the expression act in sequence to maintain the naked reference.

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.

Examples of If


IF x=+x!(x="") Do BAL

In this example, the DO executes if x contains a number or a null string.


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.


GTM>Set X=1,Y=1,Z=2 Kill UNDEF

GTM>If X=1,Y=1,Z=3,UNDEF=0 Write "HI"


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.


GTM>Set X=1 Kill UNDEF
GTM>If X=1!(UNDEF=3) Write "HI"

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.