|PL/SQL User's Guide and Reference
10g Release 1 (10.1)
Part Number B10807-01
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 Description
An expression that returns the Boolean value
NULL. Examples are comparisons for equality, greater-than, or less-than. The sequence following the THEN keyword is executed only if the expression returns
If control reaches this keyword, the sequence of statements that follows it is executed. This occurs when none of the previous conditional tests returned
Introduces a Boolean expression that is evaluated if none of the preceding conditions returned
If the expression returns
TRUE, the statements after the THEN keyword are executed.
There are three forms of
IF-THEN-ELSIF. The simplest form of
IF statement associates a Boolean expression with a sequence of statements enclosed by the keywords
IF. The sequence of statements is executed only if the expression returns
TRUE. If the expression returns
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
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
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
NULL, the sequence in the
ELSE clause is executed.
IF statement never executes more than one sequence of statements because processing is complete after any sequence of statements is executed. However, the
ELSE clauses can include more
IF statements. That is,
IF statements can be nested.
In the example below, if
shoe_count has a value of 10, both the first and second Boolean expressions return
order_quantity is assigned the proper value of 50 because processing of an
IF statement stops after an expression returns
TRUE and its associated sequence of statements is executed. The expression associated with
ELSIF is never evaluated and control passes to the
IF shoe_count < 20 THEN order_quantity := 50; ELSIF shoe_count < 30 THEN order_quantity := 20; ELSE order_quantity := 10; END IF; INSERT INTO purchase_order VALUES (shoe_type, order_quantity);
In the following example, depending on the value of
score, one of two status messages is inserted into the
IF score < 70 THEN fail := fail + 1; INSERT INTO grades VALUES (student_id, 'Failed'); ELSE pass := pass + 1; INSERT INTO grades VALUES (student_id, 'Passed'); END IF;