background image
<< Using the CASE ... WHEN Statement | Using the WHILE ... LOOP >>
<< Using the CASE ... WHEN Statement | Using the WHILE ... LOOP >>

Using Iterative Control

Controlling Program Flow
Developing and Using Stored Procedures 4-25
'Consider 4% salary increase for employee number ' || employee_id);
ELSE DBMS_OUTPUT.PUT_LINE(
'Nothing to do for employee #' || employee_id);
END CASE;
ELSE
eval_freq := 2;
END IF;
RETURN eval_freq;
END eval_frequency;
Compile and save
emp_eval
Body.
Using Iterative Control
Iteration structures, or loops, execute a sequence of statements repeatedly. There are
three basic types of loops, the
FOR...LOOP
, the
WHILE...LOOP
, and the
LOOP...EXIT WHEN
.
Using the FOR...LOOP
The
FOR...LOOP
repeats a sequence of steps a defined number of times and uses a
counter variable that must be in the defined range of integers to run the loop. The loop
counter is implicitly declared in the
FOR...LOOP
statement, and implicitly
incremented every time the loop runs. Note that the value of the loop counter can be
used within the body of the loop, but it cannot be changed programmatically. The
FOR...LOOP
statement has the following form:
FOR
counter
IN
integer_1
..
integer_2
LOOP
...
END LOOP;
Suppose that in addition to recommending that some employees receive a raise, as
described in
"Using CASE...WHEN Selection Control"
on page 4-24, function
eval_
frequency
prints how the salary for the employee would change over a set number
of years if this increase in salary continued.
Note that you will use the
DBMS_OUTPUT.PUT
procedure, described in Oracle Database
PL/SQL Packages and Types Reference.
Example 4­8 Using FOR...LOOP iterative control
In the
emp_eval
Body pane, edit
eval_frequency
function so that it uses the
proposed salary increase (
sal_raise
) that is assigned in the
CASE
block to print the
proposed salary over a number of years, starting with the current salary,
salary
. The
new code is in bold font.
FUNCTION eval_frequency (employee_id IN employees.employee_id%TYPE)
RETURN PLS_INTEGER AS
See Also:
Oracle Database PL/SQL Language Reference for more information
on
CASE...WHEN
selection control
See Also:
Oracle Database PL/SQL Language Reference for more information
on controlling
LOOP
iterations