Search code examples
plsql

PL/SQL Continue Statement


Why would the below code still produce 2 at the end? (after 1 and oracle) 'Continue' means to stop when i>1, right? Thanks.

BEGIN
for i in 1..2
  loop
  dbms_output.put_line (i);
  continue when i>1;
  dbms_output.put_line ('oracle');
  end loop;
END;

Solution

  • Continue doesn't mean to stop when i>1. You can use 'EXIT' statement to break to loop. More info about 'CONTINUE' can be found here.

    You can check the following snippet to see when the loop is broken:

    BEGIN
    for i in 1..2
      loop
      exit when i>1;
      dbms_output.put_line (i);
      dbms_output.put_line ('oracle');
      end loop;
    END;