if
Overview
The if
statement is used to conditionally execute one or more statements.
Syntax
Conditional Expressions
A conditional expression (hereafter referred to as simply an expression) is evaluated to provide a TRUE or FALSE result which in turn determines whether one or more statements are to be executed or not. The following are examples of a valid expression:
An expression used by the if
statement may contain:
Numeric and string literals
Regular expressions
Variables
Operators
Functions
Numeric and string literals
A literal is a specified value, such as 4.5
or "hostname"
. Literals may be numbers or strings (text).
If a literal is non-quoted then it will be treated as a number if it represents a valid decimal integer or floating point number (in either regular or scientific notation), else it will be treated as a string.
If a literal is quoted then it is always treated as a string, thus 3.1515926
is a number and "3.1415926"
is a string.
Regular expressions
Regular expressions must be enclosed within forwarding slashes (/
), and are assumed to be in ECMAScript format.
If present, a regular expression must be used on the right-hand side of either an !~
or an =~
operator, and when evaluated it will be applied to the value on the left-hand side of an operator, for example:
As the forward slash is used as a delimiter for the expression, any literal forward slashes required by the expression should be escaped with a backslash: \/
Variables
Variables can be used within expressions, in which case they are replaced with their values. Once expanded, these values are treated as literals.
Operators
Operators are evaluated according to the operator precedence rules in the table below (where the highest precedence is evaluated first), unless parentheses are used to override them. Operators with the same precedence are evaluated from left to right.
Although expressions are evaluated based on the precedence of each operator as listed in the above table, it is recommended that parenthesis are used within the expression in order to remove any ambiguity on the part of a future reader.
Last updated