Oracle PL/SQL:IF-THEN-ELSE Statment
IF-THEN
The most basic conditional evaluation is an IF-THEN statement. IF a condition is satisfied, THEN do the action. Implicit with this logic is IF a condition is not met, skip the code that follows and continue with the rest of the program.
The syntax for an IF-THEN statement is
IF condition
THEN
action;
END IF;
The condition can be any expression, variable, constant, or identifier compared toany other expression, variable, constant, or identifier. The condition must evaluate to TRUE, FALSE, or NULL.
If a condition is met (evaluates to TRUE), an action is performed. The action can be any valid statement, or even another nested IF-THEN statement. If separate exception handling is required inside the IF-THEN section, a nested block can be used. If a condition is not met (evaluates to FALSE or NULL), the action is skipped.
Example:
IF emp_type = ‘ENGINEER’ THEN
DBMS_OUTPUT.PUT_LINE(‘Employee Job:Engineer’);
END IF;
IF-THEN-ELSE
IF condition THEN
TRUE sequence_of_executable_statement(s)
ELSE
FALSE/NULL sequence_of_executable_statement(s)
END IF;
For example:
IF emp_type = ‘ENGINEER’ THEN
DBMS_OUTPUT.PUT_LINE(‘Employee Job:Engineer’);
ELSE
DBMS_OUTPUT.PUT_LINE(‘Employee Job:Others’);
END IF;
IF-THEN-ELSIF
IF condition-1 THEN
statements-1
ELSIF condition-N THEN
statements-N
[ELSE
ELSE statements]
END IF;
For example:
IF emp_type = ‘ENGINEER’ THEN
DBMS_OUTPUT.PUT_LINE(‘Employee Job:Engineer’);
ELSIF emp_type = ‘TEST LEAD’ THEN
DBMS_OUTPUT.PUT_LINE(‘Employee Job:Test Lead’);
ELSE
DBMS_OUTPUT.PUT_LINE(‘Employee Job:Others’);
END IF;