PL/SQL Loop – Exit

In Oracle PL/SQL, the EXIT command is used to exit a loop prematurely. It can be used in both simple loops (like a basic FOR loop) and more complex loops (like a WHILE loop or a cursor FOR loop). When the EXIT command is encountered, the loop is immediately terminated and control is transferred to the next statement after the loop.

LOOP … EXIT Loop syntax

LOOP
	--pl/sql statements
	IF condition THEN
		EXIT;
	END IF;
	--pl/sql statements
END LOOP;

LOOP … EXIT Loop example

DECLARE
  i NUMBER:=0;
BEGIN
DBMS_OUTPUT.PUT_LINE('Start');
	LOOP
		i := i + 1;
		IF i > 3 THEN
			EXIT;
		END IF;
		DBMS_OUTPUT.PUT_LINE(' i: ' || i);
	END LOOP;
DBMS_OUTPUT.PUT_LINE('End');
END;

Output

Start
 i: 1
 i: 2
 i: 3
End