Oracle PL/SQL - IF THEN ELSE statements

Introduction

IF THEN ELSIF statement

IF condition_1 THEN statements_1; 
  ELSIF condition_2 THEN statements_2; 
  ELSIF condition_3 THEN statement_3; 
END IF; 

Logically equivalent nested IF THEN ELSE statements

IF condition_1 THEN 
  statements_1; 
ELSE 
  IF condition_2 THEN 
    statements_2; 
  ELSE 
    IF condition_3 THEN 
      statements_3; 
    END IF; 
  END IF; 
END IF; 

The following code uses an IF THEN ELSIF statement with many ELSIF clauses to compare a single value to many possible values.

For this purpose, a simple CASE statement is clearer.

IF THEN ELSIF Statement Simulates Simple CASE Statement

Demo

SQL>
SQL> DECLARE-- from   w  w  w. java  2  s.c  om
  2    final_mark CHAR(1);
  3  BEGIN
  4    final_mark := 'B';
  5
  6    IF final_mark = 'A' THEN
  7      DBMS_OUTPUT.PUT_LINE('Excellent');
  8    ELSIF final_mark = 'B' THEN
  9      DBMS_OUTPUT.PUT_LINE('Very Good');
 10    ELSIF final_mark = 'C' THEN
 11      DBMS_OUTPUT.PUT_LINE('Good');
 12    ELSIF final_mark = 'D' THEN
 13      DBMS_OUTPUT. PUT_LINE('Fair');
 14    ELSIF final_mark = 'F' THEN
 15      DBMS_OUTPUT.PUT_LINE('Poor');
 16    ELSE
 17      DBMS_OUTPUT.PUT_LINE('No such final_mark');
 18    END IF;
 19  END;
 20  /
Very Good

PL/SQL procedure successfully completed.

SQL>
SQL>

Related Topic