The IF
statement executes or skips a sequence of statements, depending on the value of a Boolean expression. For more information, see Testing Conditions (IF and CASE Statements).
Keyword and Parameter Descriptions
If and only if the value of this expression is TRUE
, the statements following THEN
execute.
If control reaches this keyword, the statements that follow it execute. This occurs when no boolean_expression
had the value TRUE
.
Introduces a boolean_expression
that is evaluated if no preceding boolean_expression
had the value TRUE
.
If the expression returns TRUE
, the statements after the THEN keyword are executed.
There are three forms of IF
statements: IF-THEN
, IF-THEN-ELSE
, and IF-THEN-ELSIF
. The simplest form of IF
statement associates a Boolean expression with a sequence of statements enclosed by the keywords THEN
and END
IF
. The sequence of statements is executed only if the expression returns TRUE
. If the expression returns FALSE
or NULL
, the IF
statement does nothing. In either case, control passes to the next statement.
The second form of IF
statement adds the keyword ELSE
followed by an alternative sequence of statements. The sequence of statements in the ELSE
clause is executed only if the Boolean expression returns FALSE
or NULL
. Thus, the ELSE
clause ensures that a sequence of statements is executed.
The third form of IF
statement uses the keyword ELSIF
to introduce additional Boolean expressions. If the first expression returns FALSE
or NULL
, the ELSIF
clause evaluates another expression. An IF
statement can have any number of ELSIF
clauses; the final ELSE
clause is optional. Boolean expressions are evaluated one by one from top to bottom. If any expression returns TRUE
, its associated sequence of statements is executed and control passes to the next statement. If all expressions return FALSE
or NULL
, the sequence in the ELSE
clause is executed.
An IF
statement never executes more than one sequence of statements because processing is complete after any sequence of statements is executed. However, the THEN
and ELSE
clauses can include more IF
statements. That is, IF
statements can be nested.