Main Help → All Commands → Language Reference → If
The if
, ifStr
, ifDef
, and ifNDef
commands are used to define conditional blocks. The if
command is for numeric comparisons; the ifStr
commands is for string comparisons; the ifDef
and ifNDef
commands are to checked if variables have been defined. The format of a conditional block is:
if (num1) (op) (num2) (block of commands if numeric comparison is true) . else ifStr (string1) (op) (string2) (block of commands if string comparison is true) . else if (num1) (block of commands if (num1) is not zero) . else ifStr (string1) (block of commands if (string1) is not empty) . else ifDef #varName (block of commands if #varNam is a defined variable) . else ifDef objName (block of commands if objName in an internal script is a defined object name) . else ifNDef #varName (block of commands if #varNam is not a defined variable) . else ifNDef objName (block of commands if objName in an internal script is not a defined object name or is type None) . else ifNum #varName (block of commands if #varNam 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 of any type. If either arguments to an if
conditional are strings instead of numbers, that conditional will automatically be converted to an ifStr
command. In other words, there is no need to use ifStr
unless you explicit want to compare two strings as strings when they happen to also be valid numbers. The arguments to ifDef
and ifNDef
must be a single variable name.
Numerical 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)
.String conditionals are nearly the same except ordering is alphabetical order instead of numerical order. Furthermore the two "equals" operators provide case-insensitive or case-sensitive comparisons as follows:
=
or ' is
': true if (string1)
is equal to (string2)
by case-insensitive comparison.==
: true if (string1)
is equal to (string2)
by case-sensitive comparison (i.e., two equals signs means more equal than one equals sign).Some string functions can be used to implement other types of useful string comparisons. For example:
if offset(s1\string)>0
- is true if the string contains the string s1.if chars("\"&length(s1)&"\string")=s1
- is true if string
starts with the string s1
(case insensitive, use == to make it case sensitive).if chars((length(string)-length(s1)+1)&"\string")=s1
- is true if string
ends with the string s1
(case insensitive, use == to make it case sensitive).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.
if (num1)
), the conditional is true if the number is not equal to zero. If the number is not zero, the conditional is false.
ifStr (string1)
), the conditional is true if the string is not an empty string. If the string has text, the conditional is false.