·  PLSQL – Syntax – Conditionals – loop

 

- Info:

  - This tutorial shows how to use whilw conditional statements.

  - CONTINUE is supported only after Oracle 11g.

 

- Procedure:

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

 

    Test.sql

    SET SERVEROUTPUT ON

    

    DECLARE

      i NUMBER;

    

    BEGIN 

    

      --EXIT.          Output is "1 2" since we break out of loop when i=3.

      i := 0;

      LOOP

        i := i + 1;

        IF (i=3) EXIT; END IF;

        DBMS_OUTPUT.PUT_LINE(i);

      END LOOP;

      

      --EXIT WHEN.     Output is "1 2" since we break out of loop when i=3.

      i := 0;

      LOOP

        i := i + 1;

        EXIT WHEN (i=3);    

        DBMS_OUTPUT.PUT_LINE(i);

      END LOOP;

      

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

      i := 0;

      LOOP

        i := i + 1;

        IF (i=3) THEN CONTINUE; END IF;

        DBMS_OUTPUT.PUT_LINE(i);

      END LOOP;

 

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

      i := 0;

      LOOP

        i := i + 1;

        CONTINUE WHEN (i=3);    

        DBMS_OUTPUT.PUT_LINE(i);

      END LOOP;

      

    END;