PL/SQL

Hands On No. 5 : Conditional Statements in PL/SQL

About Conditional Statements

Conditional Statements are used when you are dealing with condition(s) and according to the result of expression or condition, you want to execute statement(s).
PL/SQL provides following conditional statements.

1. IF THEN Statement

2. IF THEN ELSE Statement

3. IF THEN ELSIF Statement

4. Simple CASE Statement

1. IF THEN Statement

The IF THEN statement either runs or skips a sequence of one or more statements, depending on a condition. The IF THEN statement has this structure:

IF condition THEN
  statement(s)-1
END IF;
Now in the above code if the condition is true then statement(s) - 1 will be executed
DECLARE 
a number; 
BEGIN 
a := 5; 
IF a>4 THEN 
 DBMS_OUTPUT.PUT_LINE('Value is greater than 4'); 
END IF; 
END;
In the given above code It will display Value is greater than 4 as value of variable a is 5

2. IF THEN ELSE Statement

The IF THEN ELSE statement has this structure:

IF condition THEN
  statement(s)-1
ELSE
  else_statements
END IF;
Now in the above code If the value of condition is true, the statement(s)-1 will run; otherwise, the else_statements will run.
DECLARE  
a number;  
BEGIN  
a := 5;  
IF a<5 THEN  
 DBMS_OUTPUT.PUT_LINE('Value is less than 5'); 
 ELSE 
  DBMS_OUTPUT.PUT_LINE('Value is greater than or equal to 5');   
END IF;  
END;
In the given above code It will display Value is greater than or equal to 5 as value of variable a is 5 so a<5 will return false hence ELSE statement will be executed.

3. IF THEN ELSIF Statement

The IF THEN ELSIF statement has this structure:

IF condition_1 THEN
  statements_1
ELSIF condition_2 THEN
  statements_2
[ ELSIF condition_3 THEN
    statements_3
]...
[ ELSE
    else_statements
]
END IF;
Now in the above code If the value of condition-1 is true, the statements_1 will run;If the value of condition-2 is true, the statements_2 will run thus way it will check all the conditions and if no condtion is matched then the else_statements will run.Even ELSE is also an optional.
DECLARE  
a number;  
BEGIN  
a := 5;  
IF a<5 THEN  
 DBMS_OUTPUT.PUT_LINE('Value is less than 5'); 
 ELSIF a>5 THEN 
  DBMS_OUTPUT.PUT_LINE('Value is greater than 5');   
  ELSE 
  DBMS_OUTPUT.PUT_LINE('Value is equal to 5');   
END IF;  
END;
In the given above code It will display Value is equal to 5 as value of variable a is 5 so a>5 will become false same way a>5 will also return false value ELSE statement will be executed.

4. CASE Statement

The simple CASE statement has this structure:

CASE selector
WHEN selector_value_1 THEN statements_1
WHEN selector_value_2 THEN statements_2
...
WHEN selector_value_n THEN statements_n
[ ELSE
  else_statements ]
END CASE;]
The simple CASE statement runs the first statements for which selector_value equals selector. Remaining conditions are not evaluated. If no selector_value equals selector, the CASE statement runs else_statements if they exist and raises the predefined exception CASE_NOT_FOUND otherwise.
DECLARE  
a number;  
BEGIN  
a := 4;  
CASE a 
    WHEN 1 THEN DBMS_OUTPUT.PUT_LINE('Monday'); 
    WHEN 2 THEN DBMS_OUTPUT.PUT_LINE('Tuesday'); 
    WHEN 3 THEN DBMS_OUTPUT.PUT_LINE('Wednesday'); 
    WHEN 4 THEN DBMS_OUTPUT.PUT_LINE('Thursday'); 
    WHEN 5 THEN DBMS_OUTPUT.PUT_LINE('Friday'); 
    WHEN 6 THEN DBMS_OUTPUT.PUT_LINE('Saturday'); 
    WHEN 7 THEN DBMS_OUTPUT.PUT_LINE('Sunday'); 
    ELSE DBMS_OUTPUT.PUT_LINE('Error'); 
END CASE; 
END;
In the given above code, value of a will be checked by CASE and it will match with WHEN 4 hence it will display Thursday. If I change the value of a to 9, it will display Error as it does not match with the given possible values of a.This is the alternative of IF THEN ELSIF Statement
DECLARE  
a number;  
BEGIN  
a := 4;  
CASE a 
    WHEN 1 THEN DBMS_OUTPUT.PUT_LINE('Monday'); 
    WHEN 2 THEN DBMS_OUTPUT.PUT_LINE('Tuesday'); 
    WHEN 3 THEN DBMS_OUTPUT.PUT_LINE('Wednesday'); 
    WHEN 4 THEN DBMS_OUTPUT.PUT_LINE('Thursday'); 
    WHEN 5 THEN DBMS_OUTPUT.PUT_LINE('Friday'); 
    WHEN 6 THEN DBMS_OUTPUT.PUT_LINE('Saturday'); 
    WHEN 7 THEN DBMS_OUTPUT.PUT_LINE('Sunday'); 
END CASE; 
EXCEPTION
  WHEN CASE_NOT_FOUND THEN
    DBMS_OUTPUT.PUT_LINE('Error');
END;
In the given above code, value of a will be checked by CASE and it will match with WHEN 4 hence it will display Thursday. If I change the value of a to 9, it will raise CASE_NOT_FOUND Exception as it does not match with the given possible values of a and will display output Error .