Oracle PL/SQL - Basic LOOP Statement

Introduction

The basic LOOP statement has this structure:

[ label ] LOOP 
    statements 
END LOOP [ label ]; 

LOOP Statements

With each iteration of the loop, the statements run and control returns to the top of the loop.

EXIT Statement

The EXIT statement exits the current iteration of a loop and transfers control to the end of either the current loop or an enclosing labeled loop.

In the following code, the EXIT statement inside the basic LOOP statement transfers control unconditionally to the end of the current loop.

Demo

SQL>
SQL> DECLARE-- from   ww w . ja v  a  2  s.  c o  m
  2    x NUMBER := 0;
  3  BEGIN
  4    LOOP
  5      DBMS_OUTPUT.PUT_LINE ('Inside loop:  x = ' || TO_CHAR(x));
  6      x := x + 1;
  7      IF x > 3 THEN
  8        EXIT;
  9      END IF;
 10    END LOOP;
 11    -- After EXIT, control resumes here
 12    DBMS_OUTPUT.PUT_LINE(' After loop:  x = ' || TO_CHAR(x));
 13  END;
 14  /
Inside loop:  x = 0
Inside loop:  x = 1
Inside loop:  x = 2
Inside loop:  x = 3
After loop:  x = 4

PL/SQL procedure successfully completed.

SQL>
SQL>

Related Topics