An ELSIF statement has the following structure: IF CONDITION 1 THEN STATEMENT 1; ELSIF CONDITION 2 THEN STATEMENT 2; ELSIF CONDITION 3 THEN STATEMENT 3; ... ELSE STATEMENT N; END IF; The reserved word IF marks the beginning of an ELSIF construct. The words CONDITION 1 through CONDITION N are a sequence of the conditions that evaluate to TRUE or FALSE. These conditions are mutually exclusive. In other words, if CONDITION 1 evaluates to TRUE, STATEMENT 1 is executed, and control is passed to the first executable statement after the reserved phrase END IF. The rest of the ELSIF construct is ignored. When CONDITION 1 evaluates to FALSE, control is passed to the ELSIF part and CONDITION 2 is evaluated, and so forth. If none of the specified conditions yield TRUE, control is passed to the ELSE part of the ELSIF construct. An ELSIF statement can contain any number of ELSIF clauses. This flow of the logic is illustrated in Figure 5.3. Figure 5.3. ESLIF Statement
Figure 5.3 shows that if condition 1 evaluates to TRUE, statement 1 is executed, and control is passed to the first statement after END IF. If condition 1 evaluates to FALSE, control is passed to condition 2. If condition 2 yields TRUE, statement 2 is executed. Otherwise, control is passed to the statement following END IF, and so forth. Consider the following example. FOR EXAMPLE DECLARE v_num NUMBER := &sv_num; BEGIN IF v_num < 0 THEN DBMS_OUTPUT.PUT_LINE (v_num' is a negative number'); ELSIF v_num = 0 THEN DBMS_OUTPUT.PUT_LINE (v_num' is equal to zero'); ELSE DBMS_OUTPUT.PUT_LINE (v_num' is a positive number'); END IF; END; The value of v_num is provided at runtime and evaluated with the help of the ELSIF statement. If the value of v_num is less that zero, the first DBMS_OUTPUT.PUT_LINE statement executes, and the ELSIF construct terminates. If the value of v_num is greater than zero, both conditions v_num < 0 and v_num = 0 evaluate to FALSE, and the ELSE part of the ELSIF construct executes. Assume that the value of v_num equals 5 at runtime. This example produces the following output: Enter value for sv_num: 5 old 2: v_num NUMBER := &sv_num; new 2: v_num NUMBER := 5; 5 is a positive number PL/SQL procedure successfully completed.
When using an ELSIF construct, it is not necessary to specify what action should be taken if none of the conditions evaluate to TRUE. In other words, an ELSE clause is not required in the ELSIF construct. Consider the following example: FOR EXAMPLE DECLARE v_num NUMBER := &sv_num; BEGIN IF v_num < 0 THEN DBMS_OUTPUT.PUT_LINE (v_num' is a negative number'); ELSIF v_num > 0 THEN DBMS_OUTPUT.PUT_LINE (v_num' is a positive number'); END IF; DBMS_OUTPUT.PUT_LINE ('Done...'); END; As you can see, there is no action specified when v_num is equal to zero. If the value of v_num is equal to zero, both conditions will evaluate to FALSE, and the ELSIF statement will not execute at all. When a value of zero is specified for v_num , this example produces the following output. Enter value for sv_num: 0 old 2: v_num NUMBER := &sv_num; new 2: v_num NUMBER := 0; Done PL/SQL procedure successfully completed.
|