Main Help → GENerel Reference → Conditional Commands
The if command is used to define conditional blocks. The if command is for numeric comparisons, but if either argument is a string, the command will automatically be converted to a string comparison or an ifStr command. The ifDef and ifNDef commands can check for a defined variable. The format of a conditional block is:
if (num1) (op) (num2) [ && (num1) (op) (num2) || (num1) (op) (num2) ... ]
(block of commands if net comparison is true)
.
else ifStr (var1) (op) (var2)
(block of commands if sting comparison is true)
(see ifStr for some special ' is ' conditionals)
.
else ifDef #varName (or use objName)
(block of commands if #varName is a defined variable)
(Internal Scripts also check if objName is defined object and not a "None" object)
.
else ifNDef #varName (or use objName)
(block of command if #varName is not a defined variable)
(Internal Scripts also check if objName not a defined object or is a "None" object)
.
else ifNum #varName
(block of command if #varName is a defined variable with a numeric value)
.
else
(block of commands if no prior condition is true)
endif
where there can be any number of conditionals and each can be a numeric comparison, a string comparison, or check for a defined variable or variable type. A single-line "if ... then" conditional option is described below.
Numeric conditionals compare (num1) to (num2) with the following comparison operators for (op):
= or == or ' is ': true if (num1) is equal to (num2).!= or <> : true if (num1) is not equal to (num2).> : true if (num1) is greater than (num2).>= : true if (num1) is greater than or equal to (num2).< : true if (num1) is less than (num2).<= : true if (num1) is less than or equal to (num2).(num1)) is true if (num1) is a number not equal to zero or if a string equal to "true" (case insensitive).If either (num1) or (num2) are non-numerical strings, the if comparison is automatically converted to an ifStr comparison (i.e., there is no need to use ifStr unless you want to force string comparison of strings having numerical values).
The if conditional block can optionally include any number of comparison groups separated by "&&" for AND or "||" for OR logicals (since 3.2/1.0). The blocks are evaluated with "&&" having precedence over "||". Although blocks cannot be grouped with parentheses, the "&&" blocks are implicitly grouped. For example, A || B && C && D || E is implicitly A || (B && C && D) || E where A through E are conditional groups. Note that conditional groups are converted to an ifStr comparison if (num1) or (num2) in any group are non-numerical strings. Although if conditionals are the only ones that can use "&&" and "||" groups, the groups can all be string comparisons.
ifDef and ifNDef should by followed by a single unquoted variable or object name and they check if that variable is defined (or not). For object names, an object defined as "None" type is treated as an undefined object.
The ifNum command can be followed by any expression but it is normally followed by a single variable and this conditional checks whether or not that variable contains a numeric value. Using non-numeric variables in numeric expressions causes an error. This conditional can be used to check variables, if needed, to trap such errors. Testing an undefined variable, however, will cause an error.
As a convenience, any conditional command can be used in a single-line version with:
if (comparison) then (command line) being identical to if (comparison) (command line) endif
with details:
(comparison) can use "&&" and "||" when they are allowed (i.e,., only for "if" commands).(command line) must be a command that completes its function as a single-line command, except it cannot be another single-line conditional.A simple test to decide if (command line) is acceptable is if the code would work by replacing the single-line conditional with the three-line version terminated with endif. For example, the command could not be a Repeat command because it cannot function as a single command. Note that command interpretation does not verify the command is allowed.