w3resource

PL/SQL Control Statement Exercises: Using nested loop with EXIT WHEN statement

PL/SQL Control Statement: Exercise-20 with Solution

Write a program in PL/SQL using nested loop with EXIT WHEN statement.

Sample Solution:

PL/SQL Code:

DECLARE
  m PLS_INTEGER := 0;
  n PLS_INTEGER := 0;
 
BEGIN
  LOOP
    m := m + 1;
    DBMS_OUTPUT.PUT_LINE ('The value of m = ' || m);
    
    LOOP
      n := n + 1;
      DBMS_OUTPUT.PUT_LINE ('The value of n = ' || n);
      EXIT WHEN (n > 4);
    END LOOP;
 
    DBMS_OUTPUT.PUT_LINE ('Exited inner loop');
 
    EXIT WHEN (m > 3);
  END LOOP;
 
  DBMS_OUTPUT.PUT_LINE ('Exited outer loop');
END;
/

Flowchart:

Flowchart: Using nested loop with EXIT WHEN statement.

Sample Output:

The value of m = 1
The value of n = 1
The value of n = 2
The value of n = 3
The value of n = 4
The value of n = 5
Exited inner loop
The value of m = 2
The value of n = 6
Exited inner loop
The value of m = 3
The value of n = 7
Exited inner loop
The value of m = 4
The value of n = 8
Exited inner loop
Exited outer loop

PL/SQL procedure successfully completed.

Improve this sample solution and post your code through Disqus

Previous: Write a program in PL/SQL to update the salary of a specifc employee by 8% if the salary exceeds the mid range of the salary against this job and update up to mid range if the salary is less than the mid range of the salary, and display a suitable message.
Next: Write a program in PL/SQL using loop with CONTINUE statement.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/plsql-exercises/control-statement/plsql-control-statement-exercise-20.php