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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758204582, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/Oracle/PL-SQL/statements/loop/index(121): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78