The EXIT and EXIT WHEN Statements : While « PL SQL Statements « Oracle PL/SQL Tutorial






The EXIT and EXIT WHEN statements enable you to escape out of the control of a loop.

The format of the EXIT loop is

EXIT;

To terminate a loop, simply follow your condition with the EXIT statement.

This is common when using IF statements.

The Syntax for the EXIT WHEN Loop:

EXIT WHEN <condition is true>;

The EXIT WHEN statement enables you to specify the condition required to exit the execution of the loop.

In this case, no IF statement is required.

SQL>
SQL> DECLARE
  2      v_Radius NUMBER := 2;
  3  BEGIN
  4      WHILE TRUE LOOP
  5           DBMS_OUTPUT.PUT_LINE('The Area is ' ||
  6                3.14 * v_Radius * v_Radius);
  7           IF v_Radius = 10 THEN
  8               EXIT;
  9           END IF;
 10            v_Radius := v_Radius + 2 ;
 11       END LOOP;
 12  END;
 13  /
The Area is 12.56
The Area is 50.24
The Area is 113.04
The Area is 200.96
The Area is 314

PL/SQL procedure successfully completed.

SQL>








22.7.While
22.7.1.WHILE Loops
22.7.2.While loop
22.7.3.WHILE LOOP with counter
22.7.4.Calculate circle Area in WHILE loop
22.7.5.Example of a WHILE loop that never executes.
22.7.6.The EXIT and EXIT WHEN Statements
22.7.7.EXIT a WHILE LOOP with exit command
22.7.8.While Counter is initialized to NULL
22.7.9.Nested WHILE Loop
22.7.10.Use WHILE Loop to insert data to table
22.7.11.A complete example of using the cursor variable using a WHILE LOOP