FOR (integer variant) v14

FOR <name> IN [REVERSE] <expression .. expression> LOOP

This form of FOR creates a loop that iterates over a range of integer values. The variable name is of type INTEGER and exists only inside the loop. The two expressions giving the loop range are evaluated once when entering the loop. The iteration step is +1.

name begins with the value of expression to the left of .. and terminates when name exceeds the value of expression to the right of ... Thus the two expressions take on the roles start-value.. end-value.

The optional REVERSE clause specifies for the loop to iterate in reverse order. The first time through the loop, name is set to the value of the right-most expression. The loop terminates when the name is less than the left-most expression.

This example uses a FOR loop that iterates from 1 to 10:

    FOR i IN 1 .. 10 LOOP
        DBMS_OUTPUT.PUT_LINE('Iteration # ' || i);

The following is the output after using the FOR statement:

Iteration # 1
Iteration # 2
Iteration # 3
Iteration # 4
Iteration # 5
Iteration # 6
Iteration # 7
Iteration # 8
Iteration # 9
Iteration # 10

If the start value is greater than the end value, the loop body doesn't execute. No error occurs, as shown by the following example:

    FOR i IN 10 .. 1 LOOP
        DBMS_OUTPUT.PUT_LINE('Iteration # ' || i);

This example has no output as the loop body never executes.


SPL also supports cursor FOR loops. See Cursor FOR loop.