Home » SQL & PL/SQL » SQL & PL/SQL » complex analyzing for this code
icon5.gif  complex analyzing for this code [message #267858] Sat, 15 September 2007 15:47 Go to next message
Hany Freedom
Messages: 256
Registered: May 2007
Location: Egypt
Senior Member

this code in the Oracle Library ..... is talk about : Using EXIT With Labeled Loops ...... I know the idea of this code , but I can't analyze the code ..... just look :-

DECLARE
s PLS_INTEGER := 0;
i PLS_INTEGER := 0;
j PLS_INTEGER;
BEGIN
<<outer_loop>>
LOOP
i := i + 1;
j := 0;
<<inner_loop>>
LOOP
j := j + 1;
s := s + i * j; -- sum a bunch of products
EXIT inner_loop WHEN (j > 5);
EXIT outer_loop WHEN ((i * j) > 15);
END LOOP inner_loop;
END LOOP outer_loop;
DBMS_OUTPUT.PUT_LINE('The sum of products equals: ' || TO_CHAR(s));
END;
/
The sum of products equals: 166

PL/SQL procedure successfully completed.


I just can't compute how the identifier S = 166
is the <<outer_loop>> compute first , or the <<inner_loop>> compute first , or together they compute in one time ?

if anyone explain to me how this code compute exactly to make the S identifier equal to 166 ...... I will be so grateful Smile

Re: complex analyzing for this code [message #267859 is a reply to message #267858] Sat, 15 September 2007 15:57 Go to previous message
Michel Cadot
Messages: 64103
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Indent the code and add some output may help to understand:
SQL> DECLARE
  2    s PLS_INTEGER := 0;
  3    i PLS_INTEGER := 0;
  4    j PLS_INTEGER;
  5  BEGIN
  6    <<outer_loop>>
  7    LOOP
  8      i := i + 1;
  9      j := 0;
 10      <<inner_loop>>
 11      LOOP
 12        j := j + 1;
 13        s := s + i * j; -- sum a bunch of products
 14        dbms_output.put_line('  Inner loop '||j||': i*j='||i*j||', s='||s);
 15        EXIT inner_loop WHEN (j > 5);
 16        EXIT outer_loop WHEN ((i * j) > 15);
 17      END LOOP inner_loop;
 18      dbms_output.put_line('Outer loop '||i||': s='||s);
 19    END LOOP outer_loop;
 20    DBMS_OUTPUT.PUT_LINE('The sum of products equals: ' || TO_CHAR(s));
 21  END;
 22  /
  Inner loop 1: i*j=1, s=1
  Inner loop 2: i*j=2, s=3
  Inner loop 3: i*j=3, s=6
  Inner loop 4: i*j=4, s=10
  Inner loop 5: i*j=5, s=15
  Inner loop 6: i*j=6, s=21
Outer loop 1: s=21
  Inner loop 1: i*j=2, s=23
  Inner loop 2: i*j=4, s=27
  Inner loop 3: i*j=6, s=33
  Inner loop 4: i*j=8, s=41
  Inner loop 5: i*j=10, s=51
  Inner loop 6: i*j=12, s=63
Outer loop 2: s=63
  Inner loop 1: i*j=3, s=66
  Inner loop 2: i*j=6, s=72
  Inner loop 3: i*j=9, s=81
  Inner loop 4: i*j=12, s=93
  Inner loop 5: i*j=15, s=108
  Inner loop 6: i*j=18, s=126
Outer loop 3: s=126
  Inner loop 1: i*j=4, s=130
  Inner loop 2: i*j=8, s=138
  Inner loop 3: i*j=12, s=150
  Inner loop 4: i*j=16, s=166
The sum of products equals: 166

PL/SQL procedure successfully completed.

Regards
Michel
Previous Topic: seeking help for desired output
Next Topic: mutating error problem
Goto Forum:
  


Current Time: Sat Dec 03 09:45:13 CST 2016

Total time taken to generate the page: 0.11515 seconds