EXIT LOOP

The PL/SQL EXIT keyword is used to exit a loop prematurely. It can be used within the loop body to exit the loop immediately and continue execution after the loop. The EXIT keyword can also be used with a label to exit a specific loop within a nested loop structure.

EXIT LOOP example

For example, consider the following code that uses a simple loop to iterate through a collection of numbers:

  
DECLARE 
	v_num NUMBER:=0;
BEGIN
	LOOP 
		v_num:=v_num+1;
		DBMS_OUTPUT.put_line(v_num);
		IF v_num = 5 THEN
			EXIT;  
		END IF;
	END LOOP;
END;

In this example, the loop iterates until the variable v_num equals 5, the EXIT command is executed, causing the loop to exit immediately and the program continues execution after the loop. The output of this program would be the numbers 1 through 5.

Output:

1
2
3
4
5