FOR-LOOP Whereas the number of iterations through a WHILE loop is unknown until
the loop completes, the number of iterations through a FOR loop is known
before the loop is entered. FOR loops iterate over a specified range of
integers. The range is part of an "iteration scheme," which is enclosed
by the keywords FOR and LOOP. The syntax follows: FOR counter IN [REVERSE] lower_bound..higher_bound LOOP sequence_of_statements;
END LOOP; The range is evaluated when the FOR loop is first entered and is never
reevaluated. As the next example shows, the sequence of statements is
executed once for each integer in the range. After each iteration, the
loop counter is incremented. FOR i IN 1..3 LOOP -- assign the values 1,2,3 to i
sequence_of_statements; -- executes three times END LOOP; If the lower bound equals the higher bound, the sequence of statements
is executed once. If the lower bound is larger than the upper bound,
the sequence of statements is not executed and control passes to the
next statement. By default, iteration proceeds upward from the lower bound to the
higher bound. However, if you use the keyword REVERSE, iteration
proceeds downward from the higher bound to the lower bound, as the example below shows. After each iteration, the loop counter is
decremented. FOR i IN REVERSE 1..3 LOOP -- assign the values 3,2,1 to i
sequence_of_statements; -- executes three times
END LOOP; Nevertheless, you write the range bounds in ascending (not descending)
order. The bounds of a loop range can be literals, variables, or expressions,
but must evaluate to integers. For example, the following iteration schemes are legal: j IN -5..5
k IN REVERSE first..last
step IN 0..TRUNC(high/low) * 2
code IN ASCII('A')..ASCII('J') As you can see, the lower bound need not be 1. However, the loop
counter increment (or decrement) must be 1. PL/SQL lets you determine the loop range dynamically at run time, as
the following example shows: SELECT COUNT(empno) INTO emp_count FROM emp; FOR i IN 1..emp_count LOOP
...
END LOOP; The value of "emp_count" is unknown at compile time; the SELECT
statement returns the value at run time. Inside a FOR loop, the loop counter can be referenced like a constant.
So, the loop counter can appear in expressions but cannot be assigned
values, as the following example shows: FOR ctr IN 1..10 LOOP
... IF NOT finished THEN
INSERT INTO ... VALUES (ctr, ...); -- legal
factor := ctr * 2; -- legal
...
ELSE
ctr := 10; -- illegal
END IF;
END LOOP; The loop counter is defined only within the loop. You cannot reference
it outside the loop. After the loop is exited, the loop counter is
undefined, as the following example shows: FOR ctr IN 1..10 LOOP
...
END LOOP;
sum := ctr - 1; -- illegal You need not declare the loop counter because it is declared implicitly
as a local variable of type INTEGER.
the loop completes, the number of iterations through a FOR loop is known
before the loop is entered. FOR loops iterate over a specified range of
integers. The range is part of an "iteration scheme," which is enclosed
by the keywords FOR and LOOP. The syntax follows: FOR counter IN [REVERSE] lower_bound..higher_bound LOOP sequence_of_statements;
END LOOP; The range is evaluated when the FOR loop is first entered and is never
reevaluated. As the next example shows, the sequence of statements is
executed once for each integer in the range. After each iteration, the
loop counter is incremented. FOR i IN 1..3 LOOP -- assign the values 1,2,3 to i
sequence_of_statements; -- executes three times END LOOP; If the lower bound equals the higher bound, the sequence of statements
is executed once. If the lower bound is larger than the upper bound,
the sequence of statements is not executed and control passes to the
next statement. By default, iteration proceeds upward from the lower bound to the
higher bound. However, if you use the keyword REVERSE, iteration
proceeds downward from the higher bound to the lower bound, as the example below shows. After each iteration, the loop counter is
decremented. FOR i IN REVERSE 1..3 LOOP -- assign the values 3,2,1 to i
sequence_of_statements; -- executes three times
END LOOP; Nevertheless, you write the range bounds in ascending (not descending)
order. The bounds of a loop range can be literals, variables, or expressions,
but must evaluate to integers. For example, the following iteration schemes are legal: j IN -5..5
k IN REVERSE first..last
step IN 0..TRUNC(high/low) * 2
code IN ASCII('A')..ASCII('J') As you can see, the lower bound need not be 1. However, the loop
counter increment (or decrement) must be 1. PL/SQL lets you determine the loop range dynamically at run time, as
the following example shows: SELECT COUNT(empno) INTO emp_count FROM emp; FOR i IN 1..emp_count LOOP
...
END LOOP; The value of "emp_count" is unknown at compile time; the SELECT
statement returns the value at run time. Inside a FOR loop, the loop counter can be referenced like a constant.
So, the loop counter can appear in expressions but cannot be assigned
values, as the following example shows: FOR ctr IN 1..10 LOOP
... IF NOT finished THEN
INSERT INTO ... VALUES (ctr, ...); -- legal
factor := ctr * 2; -- legal
...
ELSE
ctr := 10; -- illegal
END IF;
END LOOP; The loop counter is defined only within the loop. You cannot reference
it outside the loop. After the loop is exited, the loop counter is
undefined, as the following example shows: FOR ctr IN 1..10 LOOP
...
END LOOP;
sum := ctr - 1; -- illegal You need not declare the loop counter because it is declared implicitly
as a local variable of type INTEGER.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货