Control statement in PL/SQL

If statement


IF condition THEN
statements;
[ELSIF conditionTHEN
statements;]
[ELSE
statements;]
END IF;

Example

DECLARE
v_myage number:=25;
BEGIN
IF v_myage > 18
THEN
DBMS_OUTPUT.PUT_LINE('congrats you are eligible for voting  ');
END IF;
END;

Output:

ELSE statement


DECLARE
v_myage number:=15;
BEGIN
IF v_myage < 18
THEN
DBMS_OUTPUT.PUT_LINE(' Sorry you are not eligible for voting ');
ELSE
DBMS_OUTPUT.PUT_LINE(' Congrats you are eligible for voting');
END IF;
END;

Output:

ELSIF statement


DECLARE
v_grade varchar2(2):='A';
BEGIN
IF v_grade='A' THEN
DBMS_OUTPUT.PUT_LINE(' first_class ');
ELSIF v_grade ='B' THEN
DBMS_OUTPUT.PUT_LINE('2nd class ');
ELSIF v_grade ='C' THEN
DBMS_OUTPUT.PUT_LINE(' 3rd class');
ELSE
DBMS_OUTPUT.PUT_LINE(' Invalid Grade ');
END IF;
END;

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *