Kaushik Paul
Kaushik Paul

Reputation: 1

PL/SQL program to show the uses of nested loop. It seems ok, but its showing error"Encountered the symbol ">" when expecting one of the following:"

DECLARE

m PLS_INTEGER := 0; n PLS_INTEGER := 0; k PLS_INTEGER;

BEGIN

<> 
LOOP

n := n + 1; k := 0;

DBMS_OUTPUT.PUT_LINE ('The values of inner loop are: ');

<> 
LOOP

k := k + 1;

m := m + n * k; -- Sum several products



EXIT inner_loop WHEN (k > 3);

DBMS_OUTPUT.PUT_LINE ('n='||TO_CHAR(n)||' k='||TO_CHAR(k)||' m='||TO_CHAR(m));

EXIT outer_loop WHEN ((n * k) > 6); END LOOP inner_loop;

END LOOP outer_loop;

DBMS_OUTPUT.PUT_LINE

('The total sum after completing the process is: ' || TO_CHAR(m)); 

END;

/

Upvotes: 0

Views: 206

Answers (1)

Littlefoot
Littlefoot

Reputation: 143023

You wrongly labelled loops. Instead of just <> (which just means that something's different from something else), you should have used e.g. <<outer_loop>>. When fixed, your code looks like this:

SQL> set serveroutput on;
SQL> DECLARE
  2      m PLS_INTEGER := 0;
  3      n PLS_INTEGER := 0;
  4      k PLS_INTEGER;
  5  BEGIN
  6    <<outer_loop>>
  7    LOOP
  8      n := n + 1; k := 0;
  9      DBMS_OUTPUT.PUT_LINE ('The values of inner loop are: ');
 10
 11      <<inner_loop>>
 12      LOOP
 13        k := k + 1;
 14        m := m + n * k; -- Sum several products
 15
 16        EXIT inner_loop WHEN (k > 3);
 17        DBMS_OUTPUT.PUT_LINE ('n='||TO_CHAR(n)||' k='||TO_CHAR(k)||' m='||TO_CHAR(m));
 18        EXIT outer_loop WHEN ((n * k) > 6);
 19      END LOOP inner_loop;
 20    END LOOP outer_loop;
 21
 22    DBMS_OUTPUT.PUT_LINE('The total sum after completing the process is: ' || TO_CHAR(m));
 23  END;
 24  /

The values of inner loop are:
n=1 k=1 m=1
n=1 k=2 m=3
n=1 k=3 m=6
The values of inner loop are:
n=2 k=1 m=12
n=2 k=2 m=16
n=2 k=3 m=22
The values of inner loop are:
n=3 k=1 m=33
n=3 k=2 m=39
n=3 k=3 m=48
The total sum after completing the process is: 48

PL/SQL procedure successfully completed.

SQL>

Upvotes: 1

Related Questions