Search notes:

PL/SQL: LOOP statements

LOOP
  
   EXIT WHEN boolan_expression; -- terminate loop statement if boolan_expression is true
   EXIT;                        -- terminate loop statement immediately

   CONTINUE;                    -- go to next iteration
   CONTINUE WHEN …              
  
END LOOP;
FOR num IN 1 .. 5 LOOP
    FOR mun IN REVERSE 1 .. num LOOP
        …
    END LOOP;
END LOOP;
WHILE boolean_expression LOOP
    …
END LOOP;

Leaving a loop

The processing of a loop can be left prematurely with the exit statement:
begin
   for i in 1 .. 5 loop
  
       dbms_output.put_line(i);
       if i = 3 then
          exit;
       end if;
  
   end loop;
end;
With exit when, a loop can be left conditionally:
begin
   for i in 1 .. 5 loop
  
       dbms_output.put_line(i);
       exit when i = 4;
  
   end loop;
end;

Go directly to next iteration

The continue statement skips the rest of the loop body and goes directly to the next iteration of the loop:
begin
   for i in 1 .. 10 loop

       dbms_output.put(i || ' '); 
       continue when i > 5;
       dbms_output.put_line('');
   
   end loop;
   dbms_output.put_line('');
end;
/
This anonymous PL/SQL block prints:
1 
2 
3 
4 
5 
6 7 8 9 10 

Labels

Loop statements can have a label:
lp_1 LOOP

    lp_2 LOOP

        EXIT lp_1 WHEN … -- jump out of «outer» loop

    END LOOP;
      
END LOOP lp_1;

See also

Iteration control

Index