3-10. Iterating Backward Through a Range

Problem

You are working with a range of numbers and want to iterate backward through the range, from the upper bound to the lower bound.

Solution

Use a FOR loop along with the REVERSE keyword to iterate backward through the range. In this example, the same solution that was shown in Recipe 3-9 has been modified to iterate backward through the range of numbers.

BEGIN   FOR idx IN REVERSE 1..10 LOOP     DBMS_OUTPUT.PUT_LINE('The current index is: ' || idx);   END LOOP; END;

Here is the result:

The current index is: 10 The current index is: 9 The current index is: 8 The current index is: 7 The current index is: 6 The current index is: 5 The current index is: 4 The current index is: 3 The current index ...

Get Oracle and PL/SQL Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.