:IF... :ELSE... :ENDIF
Use the :IF and :ENDIF script statements to create a conditional block in your script. The script analyzes an expression and runs statements written in the block when a particular condition is met. If the condition is not met, the script either continues with the next line after the conditional block, or runs :ELSE statements if applicable.
Syntax
:IF Condition
[Statements]
[:ELSE]
[Other Statements]
:ENDIF
Parameters
-
:IF
Starts a conditional block -
Condition
Mathematical expression that determines whether to run the statements in the conditional block
Format: script literals, script variables, script functions or numbers, with the possible results true or false -
Statements
On or more script statements to execute when the Condition is true
Format: script statement -
:ELSE
Starts a branch that executes when the condition is false -
Other Statements
One or more script statements to execute when the Condition is false
Format: script statement
Notes:
- You must define the script variables that you use in a condition before the conditional block.
-
Use OR as the keyword to link a condition to several values, or to link several conditions. Write OR conditions in the following structure:
ValueComparison operatorComparison Value[OR Comparison Value...]
Example: :IF &DEP# = "EDP" OR "AE" - You can use up to 13 ORs in a Condition.
- You do not have to include :ELSE statements.
- You can nest :IF blocks to an unlimited depth.
- Trailing blank spaces are not considered in string comparisons. Therefore, blank strings always are equal independent on their length.
- Leading zeros are only ignored in numerical comparisons.
Important! The :IF statement does not recognize differences in characters beyond the first 1024 characters in a string.
A condition contains one of the following comparison operators:
Operator |
Short form |
Rule |
---|---|---|
= |
EQ |
This expression is "True" if at least one of the comparison values equals the Value. |
<> |
NE |
This expression is "True" when none of the comparison values equals the Value. |
< |
LT |
This expression is "True" if one comparison value corresponds to the defined condition. |
> |
GT |
|
<= |
LE |
|
>= |
GE |
Examples
:set &a= 'a '
:set &b= 'a'
:if &a = &b
:print 'true'
:endif
A numerical comparison takes place when both comparison values of a condition have a numerical value (data types signed, unsigned or float). In all other cases, a string comparison takes place.
The following condition is true:
:IF "0" = "0"
An alphanumeric comparison takes place when there are no numerical values.
The following condition is false:
:IF "0" = "abc"
Script lengths are not checked during the comparison process. The following condition is true because S comes before U in the alphabet:
:IF "Smith" < "UX"
In conditions that include several OR keywords, the behavior depends on the operator.
In the following example, one of the values must apply in order to meet the condition.
:IF &WD# = "MON" OR "TUE" OR "WED"
The following condition is only met if the variable does not comply with any of the three values.
:IF &WD# <> "THU" OR "FRI" OR "SAT"
See also: