·  PLSQL – Syntax – Conditionals – for

 

- Info:

  - This tutorial shows how to use for conditional statements.

  - CONTINUE is supported only after Oracle 11g.

 

- Procedure:

  - Create "PL/SQL Application" Test.sql.

 

    Test.sql

    SET SERVEROUTPUT ON

    

    BEGIN

    

      --SIMPLE.        Output is "1 2 3 4".

      FOR i IN 1..4 LOOP 

        DBMS_OUTPUT.PUT_LINE(i);

      END LOOP;

    

      --REVERSE.       Output is "4 3 2 1".

      FOR i IN REVERSE 1..4 LOOP 

        DBMS_OUTPUT.PUT_LINE(i);

      END LOOP;

 

      --EXIT.          Output is "1 2 3" since we exit out of for loop before we reach 4.

      FOR i IN 1..4 LOOP 

        DBMS_OUTPUT.PUT_LINE(i);

        IF i=3 THEN EXIT; END IF;

      END LOOP;

 

      --EXIT WHEN.     Output is "1 2 3" since we exit out of for loop before we reach 4.

      FOR i IN 1..4 LOOP 

        DBMS_OUTPUT.PUT_LINE(i);

        EXIT WHEN (i=3);

      END LOOP;

 

      --CONTINUE.      Output is "1 2 4" since display of number 3 is skipped.

      FOR i IN 1..4 LOOP 

        IF i=3 THEN CONTINUE; END IF;

        DBMS_OUTPUT.PUT_LINE(i);

      END LOOP;

            

      --CONTINUE WHEN. Output is "1 2 4" since display of number 3 is skipped.

      FOR i IN 1..4 LOOP 

        CONTINUE WHEN (i=3);

        DBMS_OUTPUT.PUT_LINE(i);

      END LOOP;

            

    END;