0% found this document useful (0 votes)
8 views

3. Control Structure- Iterative Control

The document discusses iterative control in PL/SQL, detailing the structures of Simple Loop, WHILE Loop, and FOR Loop. It provides syntax and examples for each loop type, illustrating how to execute repeated actions and manage conditions within loops. Additionally, it includes practical examples such as calculating the area of a circle and inverting a number.

Uploaded by

patelsoham.797
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

3. Control Structure- Iterative Control

The document discusses iterative control in PL/SQL, detailing the structures of Simple Loop, WHILE Loop, and FOR Loop. It provides syntax and examples for each loop type, illustrating how to execute repeated actions and manage conditions within loops. Additionally, it includes practical examples such as calculating the area of a circle and inverting a number.

Uploaded by

patelsoham.797
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Unit-3 (Stored procedures and User defined functions)

Iterative Control
 Iterative control indicates the ability to repeat or skip sections of a code block.
 A loop marks a sequence of statements that has to be repeated. The keyword loop has
to be placed before the first statement in the sequence of statements to be repeated,
while the keyword end loop is placed immediately after the last statement in the
sequence.
 Once a loop begins to execute, it will go on forever. Hence a conditional statement
that controls the number of times a loop is executed always accompanies loops.
 PL/SQL supports the following structures for iterative control:
 Simple Loop
 In simple loop, the key word loop should be placed before the first statement in
the sequence and the keyword end loop should be written at the end of the
sequence to end the loop.
 Syntax:
Loop
< Sequence of statements >
End loop;
 Example: Create a simple loop such that a message is displayed when a loop exceeds a
particular value.
DECLARE
i number := 0;
BEGIN
LOOP
i := i + 2;
EXIT WHEN i< 10;
END LOOP;
dbms_output.put_line ('Loop exited as the value of i has reached' || to_char (i));
END;
 Output:
Loop exited as the value of i has reached 12
PL/SQL procedure successfully completed.
 The WHILE loop
 Syntax:
WHILE<Condition>
LOOP
< Action >
END LOOP;
 Example 3: Write a PL/SQL code block to calculate the area of a circle for a value of radius
varying from 3 to 7. Store the radius and the corresponding values of calculated area in
an empty table named Areas, consisting of two columns Radius and Area.
 Table Name: Areas
RADIUS AREA
 CREATE TABLE AREAS (RADIUS NUMBER(5), AREA NUMBER(14,2));
DECLARE
pi constant number(4,2) := 3.14 ;
radius number(5);
area number( 14,2);
BEGIN
radius := 3;
WHILE RADIUS <= 7
LOOP
area := pi * power(radius,2);
INSERT INTO areas VALUES (radius, area);
radius := radius + 1;
END LOOP;
END;
 After the loop is completed the table will now hold the following:
Table Name: Areas
RADIUS AREA
3 28.26
4 50.24
5 78.5
6 113.04
7 153.86
 The FOR Loop
 Syntax:
FOR variable IN [REVERSE] start..end
LOOP
< Action >
END LOOP;
 Note
 The variable in the For Loop need not be declared. Also the increment value cannot be
specified. The For Loop variable is always incremented by 1.
 Example: Write a PL/SQL block of code for inverting a number 5639 to 9365.
DECLARE
given_number varchar (5) := '5639';
str_length number(2);
inverted_number varchar(5);
BEGIN
Str_length := length(given_number);
FORcntr IN REVERSE 1..str_length
/* Variables used as counter in the for loop need not be declared i.e. cntr declaration is not
required */
LOOP
inverted_number := inverted_number || substr(given_number, cntr, 1);
END LOOP;
dbms_output.put_line (The Given number is ' || given_number);
dbms_output.put_line (The Inverted number is ' || inverted_number);
END;
 Output:
The Given number is 5639
The Inverted number is 9365

You might also like