PL/SQL CONTINUE Statement βοΈ¶
Mentor's Note:
EXITkills the whole loop.CONTINUEis 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.