Oracle PL/SQL Tutorial - PL/SQL CONTINUE






CONTINUE statement skips the current iteration of the loop immediately and jumps to the next iteration of the loop.


DECLARE -- from  w  w  w  . jav  a 2  s .  c o m
  x NUMBER := 0; 
BEGIN 
  LOOP
    DBMS_OUTPUT.PUT_LINE (x); 
    x := x + 1; 
 
    IF x < 5 THEN 
      CONTINUE; 
    END IF; 
 
    DBMS_OUTPUT.PUT_LINE ('-'); 
    DBMS_OUTPUT.PUT_LINE (x); 
 
    EXIT WHEN x = 10; 
  END LOOP; 
 
  DBMS_OUTPUT.PUT_LINE (' After loop:  x = ' || TO_CHAR(x)); 
END; 
/ 

The code above generates the following result.





CONTINUE-WHEN Statement

CONTINUE-WHEN statement has the condition in the WHEN clause.

If the condition is true, the current iteration of the loop is skipped.


DECLARE -- from  www.  j a  va 2  s . co m
  x NUMBER := 0; 
BEGIN 
  LOOP
    DBMS_OUTPUT.PUT_LINE (x); 
    x := x + 1; 
    CONTINUE WHEN x < 5; 
    DBMS_OUTPUT.PUT_LINE ('after continue'); 
    DBMS_OUTPUT.PUT_LINE (x); 
    EXIT WHEN x = 10; 
  END LOOP; 
  DBMS_OUTPUT.PUT_LINE (' After loop:  x = ' || TO_CHAR(x)); 
END; 
/ 

The code above generates the following result.





Example

Here is a simple example of using CONTINUE WHEN to skip over loop body execution for even numbers:


BEGIN 
   FOR l_index IN 1 .. 20 
   LOOP 
      CONTINUE WHEN MOD (l_index, 2) = 0; 
      DBMS_OUTPUT.PUT_LINE ('Loop index = ' || TO_CHAR (l_index)); 
   END LOOP; 
END; 
/ 

The code above generates the following result.