Numeric FOR loop

In PL/SQL there is a "classic" for loop, where an integer is used to delimit the range in which the loop has to be executed:

FOR index IN [REVERSE] n .. m
LOOP
(...)
END LOOP;

Where n and m represent the interval limits. Notice that n should be less or equal to m, even in case we specify the REVERSE option. In case n equals m, the loop would be executed just once. If n is greater than m, the control goes after the end of the loop.

Here is a FOR loop that prints the number 1, 2 and 3, each on its own line:

for my_value in 1 .. 3
loop
dbms_output.put_line(my_value);
end loop;

And here is a countdown:

for my_value in reverse 1 .. 3
loop
dbms_output.put_line(my_value);
end loop;


The fifth chapter of Oracle PL/SQL Programming, fifth edition, by Steven Feuerstein is about loops.

No comments:

Post a Comment