13.33 FOR LOOP Statement
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, or when a statement inside the loop transfers control outside the loop or raises an exception.
Topics
Syntax
for_loop_statement ::=
Semantics
for_loop_statement
index
Name for the implicitly declared integer variable that is local to the FOR
LOOP
statement. Statements outside the loop cannot reference index
. Statements inside the loop can reference index
, but cannot change its value. After the FOR
LOOP
statement runs, index
is undefined.
See Also:
[ REVERSE ] lower_bound .. upper_bound
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 thanupper_bound
, thestatements
do not run, and control transfers to the statement after theFOR
LOOP
statement.Otherwise,
lower_bound
is assigned toindex
, thestatements
run, and control returns to the top of the loop, whereindex
is compared toupper_bound
. Ifindex
is less thanupper_bound
,index
is incremented by one, thestatements
run again, and control returns to the top of the loop. Whenindex
is greater thanupper_bound
, control transfers to the statement after theFOR
LOOP
statement. -
If
REVERSE
is specified:If
upper_bound
is less thanlower_bound
, thestatements
do not run, and control transfers to the statement after theFOR
LOOP
statement.Otherwise,
upper_bound
is assigned toindex
, thestatements
run, and control returns to the top of the loop, whereindex
is compared tolower_bound
. Ifindex
is greater thanlower_bound
,index
is decremented by one, thestatements
run again, and control returns to the top of the loop. Whenindex
is less thanlower_bound
, control transfers to the statement after theFOR
LOOP
statement.
label
A label that identifies for_loop_statement
(see "statement ::=" and "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 in the END
LOOP
statement matches a label at the beginning of the same LOOP
statement (the compiler does not check).
Examples
-
Example 4-15, "FOR LOOP Statements"
-
Example 4-16, "Reverse FOR LOOP Statements"
-
Example 4-17, "Simulating STEP Clause in FOR LOOP Statement"
-
Example 4-19, "Outside Statement References FOR LOOP Statement Index"
-
Example 4-20, "FOR LOOP Statement Index with Same Name as Variable"
-
Example 4-21, "FOR LOOP Statement References Variable with Same Name as Index"
-
Example 4-22, "Nested FOR LOOP Statements with Same Index Name"
-
Example 4-23, "FOR LOOP Statement Bounds"
-
Example 4-24, "Specifying FOR LOOP Statement Bounds at Run Time"
Related Topics
In this chapter:
In other chapters: