Nested "IF" Statements

0
Nested "IF" Statements:
  • The "IF" statements can be nested into one another as per requirements.
  • Nested "IF" is a situation in which an "IF" follows another "IF" immediately for every "TRUE" state of an "IF" condition.
  • Each "IF" is considered as an individual block of "IF" and needs proper nesting.
  • Each "IF" block that is opened needs a proper close with "END IF". Else PL/SQL raises exceptions.
  • Nested "IF" situations have to be planned when we have a series of conditions fall sequentially.
Syntax:
IF condition THEN
    IF condition THEN
        IF condition THEN
        Statement1;
        ELSE
        Statement2;
        END IF;
    ELSE
    Statement3;
    END IF;
ELSE
Statement4;
END IF;


SQL>DECLARE
    v_year NUMBER:=&year;
    BEGIN
    IF mod(v_year,4)=0 THEN
    IF mod(v_year,100)<>0 THEN
    DBMS_OUTPUT.PUT_LINE(v_year||'is a leap year');
    ELSE
        IF mod(v_year,400)=0 THEM
        DBMS_OUTPUT.PUT_LINE(v_year||'is a leap year');   
        ELSE
        DBMS_OUTPUT.PUT_LINE(v_year||'is a not a leap year');
        END IF;
        END IF;
    ELSE
    DBMS_OUTPUT.PUT_LINE(v_year||'is a not leap year');
    END IF;
    END;

About the author

Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus. Aenean fermentum, eget tincidunt.

0 comments:

Recent Posts