Home » PLSQL » Using CONTINUE In Loops To Resume Control In Oracle

Using CONTINUE In Loops To Resume Control In Oracle

The CONTINUE statement in loops exits the current iteration of a loop unconditionally and transfers control to the next iteration of either the current loop or an enclosing labeled loop.
In below example, the CONTINUE statement inside the basic LOOP statement transfers control unconditionally to the next iteration of the current loop.

DECLARE
   x   NUMBER := 0;
BEGIN
   LOOP                      -- After CONTINUE statement, control resumes here
      DBMS_OUTPUT.PUT_LINE ('Inside loop: x = ' || TO_CHAR (x));
      x := x + 1;

      IF x < 3  -- instead this if end if condition for continue you can use also CONTINUE When x < 3;
      THEN
         CONTINUE;
      END IF;

      DBMS_OUTPUT.PUT_LINE (
         'Inside loop, after CONTINUE: x = ' || TO_CHAR (x));
      EXIT WHEN x = 5;
   END LOOP;

   DBMS_OUTPUT.PUT_LINE (' After loop: x = ' || TO_CHAR (x));
END;
/
The output of the above block should come something like this:

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