Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: PL/SQL question regarding nested loops
You forgot to reset J to 1 before
> while j <= 2 loop
and you should put
> exit when i = 3;
after the first
> end loop;
Ciao
Eugenio
B. Williams wrote:
> I have some code that uses for loops and I chaged the outer loop into a
> simple loop and inner loop into a while loop, but the out put is coming out
> different. The outer loop is supposed to run 3 times while the inner loops
> is supposed to run 6 times. Wouls someone look at my code and tell me what I
> have done wrong. Thanks in advance.
>
> Original code
> set serveroutput on
> declare
> v_test NUMBER := 0;
> begin
> <<outer_loop>>
> for i IN 1..3 loop
> dbms_output.put_line('Outer Loop');
> dbms_output.put_line('i = ' || i);
> dbms_output.put_line('v_test = ' || v_test);
> v_test := v_test + 1;
> <<inner_loop>>
> for j IN 1..2 loop
> dbms_output.put_line('Inner Loop');
> dbms_output.put_line('j = ' || j);
> dbms_output.put_line('i = ' || i);
> dbms_output.put_line('v_test = ' || v_test);
> end loop Inner_Loop;
> end loop outer_loop;
> end;
> /
>
> Modified code
>
> set serveroutput on
> declare
> i PLS_INTEGER := 0;
> j PLS_INTEGER := 1;
> v_test NUMBER := 0;
> begin
> loop
> i := i + 1;
> dbms_output.put_line('Outer Loop');
> dbms_output.put_line('i = ' || i);
> dbms_output.put_line('v_test = ' || v_test);
> v_test := v_test + 1;
> exit when i = 3;
> while j <= 2 loop
> dbms_output.put_line('Inner Loop');
> dbms_output.put_line('j = ' || j);
> dbms_output.put_line('i = ' || i);
> dbms_output.put_line('v_test = ' || v_test);
> j := j + 1;
> end loop;
> end loop;
> end;
> /
Received on Fri Aug 04 2006 - 05:58:24 CDT