Oracle PL/SQL - CONTINUE WHEN Statement

Introduction

The CONTINUE WHEN statement exits the current iteration of a loop when the condition in its WHEN clause is true.

It transfers control to the next iteration of either the current loop or an enclosing labeled loop.

Each time control reaches the CONTINUE WHEN statement, the condition in its WHEN clause is evaluated.

If the condition is not true, the CONTINUE WHEN statement does nothing.

In the following code, the CONTINUE WHEN statement inside the basic LOOP statement transfers control to the next iteration of the current loop when x is less than 3.

Demo

SQL>
SQL> DECLARE-- from ww  w  . ja  va 2s .c om
  2    x NUMBER := 0;
  3  BEGIN
  4    LOOP -- After CONTINUE statement, control resumes here
  5      DBMS_OUTPUT.PUT_LINE ('Inside loop:  x = ' || TO_CHAR(x));
  6      x := x + 1;
  7      CONTINUE WHEN x < 3;
  8      DBMS_OUTPUT.PUT_LINE('Inside loop, after CONTINUE:  x = ' || TO_CHAR(x));
  9      EXIT WHEN x = 5;
 10    END LOOP;
 11    DBMS_OUTPUT.PUT_LINE (' After loop:  x = ' || TO_CHAR(x));
 12  END;
 13  /
Inside loop:  x = 0
Inside loop:  x = 1
Inside loop:  x = 2
Inside loop, after CONTINUE:  x = 3
Inside loop:  x = 3
Inside loop, after CONTINUE:  x = 4
Inside loop:  x = 4
Inside loop, after CONTINUE:  x = 5
After loop:  x = 5

PL/SQL procedure successfully completed.

SQL>
SQL>

Related Topic