Oracle® Database PL/SQL Language Reference 11g Release 2 (11.2) Part Number E10472-02 |
|
|
View PDF |
With each iteration of the FOR
LOOP
statement, its statements run, its index is either incremented or decremented, and control returns to the top of the loop. The FOR
LOOP
statement ends when its index reaches a specified value, when a statement inside the loop transfers control outside the loop, or when an exception is raised.
Topics:
Syntax
for_loop_statement ::=
See statement ::=.
Semantics
index_name
An identifier for the implicitly declared integer variable that is local to the FOR
LOOP
statement. Statements outside the loop cannot reference index_name
. Statements inside the loop can reference index_name
, but cannot change its value. After the FOR
LOOP
statement runs, index_name
is undefined.
See Also:
"FOR LOOP Index"label
A label that identifies for_loop_statement
(see label). CONTINUE
, EXIT
, and GOTO
statements can reference this label.
Labels improve readability, especially when LOOP
statements are nested, but only if you ensure that the label at the end of the LOOP
statement matches one of the labels at the beginning of the same statement (the compiler does not check).
[ REVERSE ] lower_bound .. upper_bound
The lower_bound
and upper_bound
must evaluate to numbers (see "Lower Bound and Upper Bound"). PL/SQL evaluates lower_bound
and upper_bound
once, when the FOR
LOOP
statement is entered, and stores them as temporary PLS_INTEGER
values, rounding them to the nearest integer if necessary.
If lower_bound
equals upper_bound
, the statements
run only once.
If lower_bound
does not equal upper_bound
when the FOR
LOOP
statement begins to run, then:
If REVERSE
is omitted:
If lower_bound
is greater than upper_bound
, the statements
do not run, and control transfers to the statement after the FOR
LOOP
statement.
Otherwise, lower_bound
is assigned to index
, the statements
run, and control returns to the top of the loop, where index
is compared to upper_bound
. If index
is less than upper_bound
, index
is incremented by one, the statements
run again, and control returns to the top of the loop. When index
is greater than upper_bound
, control transfers to the statement after the FOR
LOOP
statement.
If REVERSE
is specified:
If upper_bound
is less than lower_bound
, the statements
do not run, and control transfers to the statement after the FOR
LOOP
statement.
Otherwise, upper_bound
is assigned to index
, the statements
run, and control returns to the top of the loop, where index
is compared to lower_bound
. If index
is greater than lower_bound
, index
is decremented by one, the statements
run again, and control returns to the top of the loop. When index
is less than lower_bound
, control transfers to the statement after the FOR
LOOP
statement.
Examples
Example 4-23, "Changing the Increment of the Counter in a FOR LOOP Statement"
Example 4-18, "Statement Outside FOR LOOP Tries to Reference Index"
Example 4-19, "FOR LOOP Index with Same Name as Declared Variable"
Example 4-20, "FOR LOOP References Declared Variable with Same Name as Index"
Example 4-21, "Nested FOR LOOP Statements with Same Index Name"
Related Topics
In this chapter:
In other chapters: