Tuesday, October 12, 2004

PL/SQL For Loops

Even when we use reverse option in for loop the intial counter should always be less than the last counter.

For example the following code will never execute

for x in reverse 12..1
loop
dbms_output.put_line('You can never read me');
end loop;


We can manipulate the range in the loops but they do not effect the execution of the loop in any way because the range scheema is executed only the time the loop begins. Any changes made to the range variables will not effect the range ounter. Its considered bad programming practice though.

FOR a_counter IN lo_val .. hi_val

LOOP
IF a_counter > lo_val * 2
THEN
hi_val := lo_val;
END IF;
END LOOP;
END LOOP;

No comments: