Syntax

EXIT [ WHEN <expression> ];

The innermost loop is terminated, and the statement following END LOOP is executed next.

If WHEN is present, loop exit occurs only if the specified condition is TRUE. Otherwise control passes to the statement after EXIT.

You can use EXIT to cause early exit from all types of loops, not just unconditional loops.

Example

This example shows a loop that iterates 10 times and then uses the EXIT statement to terminate:

DECLARE
    v_counter       NUMBER(2);
BEGIN
    v_counter := 1;
    LOOP
        EXIT WHEN v_counter > 10;
        DBMS_OUTPUT.PUT_LINE('Iteration # ' || v_counter);
        v_counter := v_counter + 1;
    END LOOP;
END;

The following is the output from this example:

Output
Iteration # 1
Iteration # 2
Iteration # 3
Iteration # 4
Iteration # 5
Iteration # 6
Iteration # 7
Iteration # 8
Iteration # 9
Iteration # 10