Previous | Table of Contents | Next

Page 116

Using IF...ELSIF

In all the examples so far, you were coding IF statements in an AND environment. You might often want to check a value against a series of conditions, which have distinct boundaries. In a third-generation language, you would think of this as a CASE statement, or in boolean logic, you would be separating the conditions with a logical OR.

The Syntax for IF...ELSIF

IF <condition1_evaluates_to_true>
THEN
    <perform_statements>
ELSIF <condition2_evaluates_to_true>
THEN
     <perform_statements>
ELSIF <condition3_evaluates_to_true>
THEN
     <perform_statements>
…
ELSE <this is always optional as the default value>
     <perform_statements>
END IF;

In this syntax, the IF...ELSIF statement acts like a logical OR statement. The first parameter, condition1_evaluates_to_true, is a BOOLEAN condition. If it evaluates to true, then one or more statements are executed at the perform_statements parameter. You will keep adding as many ELSIF statements as required for all the conditions. You will notice that only one END IF statement is required, unlike the other IF...THEN...ELSE statements. Also note that the ELSE statement is optional, and it acts as a default value if none of the other values are true.

TIP
When utilizing ELSIF, do not allow for any overlapping of values to evaluate because you will not get the desired result. If you were checking for grades, you would not want to check for values of between 70 and 80 for a C and then check for values of 80 to 90 for a B. The person who received an 80 percent would be upset at a grade of C instead of the letter grade she deserved of B!

Listing 5.11 is an example of using ELSIF to determine the grade letter for a student. You have several approaches. One would be to check a range using BETWEEN; another method would be to use the > or < for evaluation, and finally, you could use a default letter `E' or have it as part of the conditional criteria. To ensure no overlapping, the following example will use the < sign, and to practice a default, use the letter `E' for failure.

Page 117

INPUT
Listing 5.11. Using IF...ELSIF to determine a grade.

DECLARE
v_Score Number := 85; --Percentage
v_LetterGrade Char(1);
BEGIN
IF v_Score >= 90 THEN
     v_LetterGrade := `A';
ELSIF v_Score >= 80 THEN
     v_LetterGrade := `B';
ELSIF v_Score >= 70 THEN
     v_LetterGrade := `C';
ELSIF v_Score >= 60 THEN
     v_LetterGrade := `D';
ELSE
     v_LetterGrade := `E';
END IF;
     DBMS_OUTPUT.PUT_LINE(`Your Letter Grade is: ` || v_LetterGrade);
END;
/

OUTPUT
When you execute the code from Listing 5.11, your output will be

Your Letter Grade is B

Remember, IF...ELSIF will continue through all cases until the first evaluates to true, and then the rest of the statements are ignored. Had you started out with v_Score >= 60 , 85 would have evaluated to true and the student would have received a v_LetterGrade of `D'.

Nested IF Versus ELSIF

You could think of nested IF statements as performing a logical AND whereas ELSIF would be performing a logical OR. The nice feature about using ELSIF instead of nested IFs is that it is much easier to follow the logic in the ELSIF statement because you can easily identify which statements will occur under which logical conditions.

The Formatting of IF Statements

Although there are no specific rules set in stone on placement of IF...THEN...ELSIF, and so forth, there are some general rules, which will help make your code more readable and easy to follow. See Listing 5.12 for an example of proper formatting.

Page 118

Listing 5.12. Formatting IF statements.

 1: IF v_HoursWorked > 40 THEN
 2:      IF v_PayType = `H' THEN
 3:           v_OverTime := v_HoursWorked - 40;
 4:           DBMS_OUTPUT.PUT_LINE(`The many Hours which have been worked
 5:                overtime= ` || v_OverTime);
 6:      ELSE
 7:           IF v_PayType = `S' THEN
 8:                DBMS_OUTPUT.PUT_LINE(`Employee is Salaried');
 9:           ELSE
10:                DBMS_OUTPUT.PUT_LINE(`Employee is Executive Management');
11:           END IF;
12:      END IF; -- End check for PayType = H
13: END IF;
14: END;
15: /

Avoiding Common Mistakes When Using IF

You can avoid some of the more common pitfalls when using IF statements by keeping this list in mind.

Do Don't
Do make sure that every IF statement has a matching END IF statement.
Do make sure that you spell ELSIF without the extra E (as in ELSEIF).
Do make sure that you place a space between the END IF statement instead of either no space or a dash.
Don't make nested loops too complex. Complexity will make it harder to follow and debug if problems or changes occur. Evaluate your logic and see if a function might accomplish the same task.
Don't forget your punctuation. You do need semicolons after END IF and after each of the statements, but not after the keyword THEN.

Previous | Table of Contents | Next

Используются технологии uCoz