Skip to content

PL/SQL CONTINUE Statement ⏭️

Mentor's Note: EXIT kills the whole loop. CONTINUE is differentβ€”it just says, "I'm done with this specific turn, let's move to the next one." It's like skipping a song in a playlist rather than turning off the music player. πŸ’‘


🌟 The Scenario: The Bad Apple 🍎

Imagine you are sorting a basket of 10 apples. - Task: Wash each apple. - Logic: If an apple is rotten, Skip it and move to the next one. Don't stop washing the other good apples! βœ…


πŸ’» 1. Basic CONTINUE

Skips the rest of the code in the current loop and jumps back to the top.

BEGIN
   FOR i IN 1..5 LOOP
      IF i = 3 THEN
         CONTINUE; -- ⏭️ Skip number 3
      END IF;

      DBMS_OUTPUT.PUT_LINE('Processing number: ' || i);
   END LOOP;
END;
-- Result: 1, 2, 4, 5 (3 is missing!)

πŸ’» 2. The CONTINUE WHEN Clause (Shorthand)

A cleaner way to write conditional skipping.

BEGIN
   FOR i IN 1..10 LOOP
      -- πŸš€ Logic: Skip even numbers
      CONTINUE WHEN MOD(i, 2) = 0;

      DBMS_OUTPUT.PUT_LINE('Odd Number: ' || i);
   END LOOP;
END;

πŸ“Š Comparison: EXIT vs. CONTINUE

Command Action Analogy
EXIT Terminates the loop πŸ›‘ Closing the store
CONTINUE Skips current iteration ⏭️ Skipping one customer

πŸ—οΈ Architect's Note: Performance πŸ›‘οΈ

CONTINUE was introduced in Oracle 11g. In older versions, developers had to use complex nested IF statements or GOTO to skip logic. - Tip: Use CONTINUE WHEN to make your loops cleaner and more readable for teammates.


πŸ“ˆ Learning Path