Skip to main content

IF...THEN...ELSE

Conditionally executes a group of statements, depending on the value of an expression.

Synopsis

IF condition THEN statements
IF condition ELSE elsestatements
IF condition THEN statements ELSE elsestatements

IF condition
   [THEN
    statements
END]
   [ELSE
    elsestatements
END]

Arguments

condition An expression that evaluates to True or False. For further details on boolean logical operators, refer to the Operators page of this manual.
statements One or more statements executed if condition is True.
elsestatements One or more statements executed if no previous condition expression is True.

Description

The IF statement performs a boolean test on condition, and then executes either the THEN clause (condition=1 (true)) or the ELSE clause (condition=0 (false)).

You can omit or include either the THEN clause or the ELSE clause. If condition=1 and the THEN clause is omitted, or condition=0 and the ELSE clause is omitted, IF returns the empty string. Further IF statements can be nested within THEN or ELSE clauses.

IF can be coded as a single-line statement, or as a code block statement using the END keyword. You can use any of the single-line forms for short, simple tests. However, the block form provides more structure and flexibility than the single-line form and is usually easier to read, maintain, and debug.

When executing a block IF, condition is tested. If condition is True, the statements following THEN are executed. If condition is False, the statements following ELSE are executed. After executing the statements following THEN or ELSE, execution continues with the statement following END.

What follows the THEN keyword is examined to determine whether or not a statement is a block IF. If anything other than a comment appears after THEN on the same line, the statement is treated as a single-line IF statement.

For a block IF statement, the IF keyword must be the first statement on a line. The block IF must end with an END statement.

The condition expression can be a compound expression, using = (equal to), # (not equal to), and other the comparison operators. You can use literals, variables, and dynamic arrays as condition expression elements. Multiple test expressions can be associated by AND and OR logical operators.

See Also

FeedbackOpens in a new tab