Unit 2 LECTURE 3.2 (Iterative Control Structure)
Unit 2 LECTURE 3.2 (Iterative Control Structure)
CHAPTER 2.3
LECTURE 3.2( Iterative Control Structure)
LOOP statements let you execute a sequence of statements multiple times. There are three
forms of LOOP statements: LOOP, WHILE-LOOP, and FOR-LOOP.
LOOP
The simplest form of LOOP statement is the basic (or infinite) loop, which encloses a
sequence of statements between the keywords LOOP and END LOOP, as follows:
LOOP
sequence_of_statements
END LOOP;
With each iteration of the loop, the sequence of statements is executed, then control resumes
at the top of the loop. If further processing is undesirable or impossible, you can use
an EXIT statement to complete the loop. You can place one or more EXIT statements
anywhere inside a loop, but nowhere outside a loop. There are two forms
of EXIT statements: EXIT and EXIT-WHEN.
EXIT
The EXIT statement forces a loop to complete unconditionally. When an EXIT statement is
encountered, the loop completes immediately and control passes to the next statement. An
example follows:
LOOP
...
...
END IF;
END LOOP;
The next example shows that you cannot use the EXIT statement to complete a PL/SQL
block:
BEGIN
...
...
END IF;
END;
Remember, the EXIT statement must be placed inside a loop. To complete a PL/SQL block
before its normal end is reached, you can use the RETURN statement.
EXIT-WHEN
The EXIT-WHEN statement lets a loop complete conditionally. When the EXIT statement is
encountered, the condition in the WHEN clause is evaluated. If the condition is true, the loop
completes and control passes to the next statement after the loop. An example follows:
LOOP
...
END LOOP;
CLOSE c1;
Until the condition is true, the loop cannot complete. So, a statement inside the loop must
change the value of the condition. In the last example, if the FETCH statement returns a row,
the condition is false. When the FETCH statement fails to return a row, the condition is true,
the loop completes, and control passes to the CLOSE statement.
The EXIT-WHEN statement replaces a simple IF statement. For example, compare the
following statements:
EXIT;
END IF;
These statements are logically equivalent, but the EXIT-WHEN statement is easier to read
and understand.
Loop Labels
Like PL/SQL blocks, loops can be labeled. The label, an undeclared identifier enclosed by
double angle brackets, must appear at the beginning of the LOOP statement, as follows:
<<label_name>>
LOOP
sequence_of_statements
END LOOP;
Optionally, the label name can also appear at the end of the LOOP statement, as the following
example shows:
<<my_loop>>
LOOP
...
When you nest labeled loops, use ending label names to improve readability.
With either form of EXIT statement, you can complete not only the current loop, but any
enclosing loop. Simply label the enclosing loop that you want to complete. Then, use the
label in an EXIT statement, as follows:
<<outer>>
LOOP
...
LOOP
...
END LOOP;
...
WHILE-LOOP
sequence_of_statements
END LOOP;
Before each iteration of the loop, the condition is evaluated. If the condition is true, the
sequence of statements is executed, then control resumes at the top of the loop. If the
condition is false or null, the loop is bypassed and control passes to the next statement. An
example follows:
...
END LOOP;
The number of iterations depends on the condition and is unknown until the loop completes.
The condition is tested at the top of the loop, so the sequence might execute zero times. In the
last example, if the initial value of total is larger than 25000, the condition is false and the
loop is bypassed.
Some languages have a LOOP UNTIL or REPEAT UNTIL structure, which tests the
condition at the bottom of the loop instead of at the top. Therefore, the sequence of
statements is executed at least once. PL/SQL has no such structure, but you can easily build
one, as follows:
LOOP
sequence_of_statements
END LOOP;
To ensure that a WHILE loop executes at least once, use an initialized Boolean variable in
the condition, as follows:
done := FALSE;
sequence_of_statements
done := boolean_expression;
END LOOP;
A statement inside the loop must assign a new value to the Boolean variable. Otherwise, you
have an infinite loop. For example, the following LOOP statements are logically equivalent:
... | ...
WHILE loop
The while loop executes commands in its body as long as the condtion remains true
Syntax :
LOOP
END LOOP
DECLARE
num Number(3) :=123;
i Number(3) :=0;
BEGIN
WHILE num != 0
LOOP
i:=mod(num,10);
ans:=(ans * 10 ) + i;
num:=floor(num/10);
END LOOP;
END;
Output :
SQL>set serveroutput on
SQL>start d://rev.sql
Example 2:Write a program in PL/SQL to print the value of a variable inside and
outside a loop using LOOP EXIT statement.
DECLARE
n NUMBER := 0;
BEGIN
LOOP
n := n + 1;
IF n > 5 THEN
EXIT;
END IF;
END LOOP;
END;
Sample Output:
SQL> /
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. A double dot (..) serves as the
range operator. The syntax follows:
sequence_of_statements
END LOOP;
The range is evaluated when the FOR loop is first entered and is never re-evaluated.
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.
END LOOP;
The following example shows that if the lower bound equals the higher bound, the sequence
of statements is executed once:
END LOOP;
By default, iteration proceeds upward from the lower bound to the higher bound. However, as
the example below shows, if you use the keyword REVERSE, iteration proceeds downward
from the higher bound to the lower bound. After each iteration, the loop counter is
decremented. Nevertheless, you write the range bounds in ascending (not descending) order.
END LOOP;
Inside a FOR loop, the loop counter can be referenced like a constant but cannot be assigned
values, as the following example shows:
ELSE
END IF;
END LOOP;
Simple FOR loops iterate over a specified range of integers. The number of iterations is
known before the loop is entered. A double dot (..) serves as the range operator. The range is
evaluated when the FOR loop is first entered and is never re-evaluated. If the lower bound
equals the higher bound, the loop body is executed once.
DECLARE
p NUMBER := 0;
BEGIN
p := p + ( ( (-1) ** (k + 1) ) / ((2 * k) - 1) );
END LOOP;
p := 4 * p;
END;
DECLARE
n number:= &first_n_number;
i number:=1;
m number:=1;
BEGIN
m:=m+3;
dbms_output.put(m||' ');
END LOOP;
dbms_output.new_line;
END;
Sample Output:
1 4 7 10 13 16 19 22 25 28
Suggested References