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

PL SQL Loops

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

PL SQL Loops

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

PL/SQL - Loops

There may be a situation when you need to execute a block of code several
number of times. In general, statements are executed sequentially: The
first statement in a function is executed first, followed by the second, and
so on.

Programming languages provide various control structures that allow for


more complicated execution paths.

A loop statement allows us to execute a statement or group of statements


multiple times and following is the general form of a loop statement in most
of the programming languages −

PL/SQL provides the following types of loop to handle the looping


requirements. Click the following links to check their detail.

S.No Loop Type & Description

PL/SQL Basic LOOP


In this loop structure, sequence of statements is enclosed between the LOOP and the
1
END LOOP statements. At each iteration, the sequence of statements is executed and
then control resumes at the top of the loop.

PL/SQL WHILE LOOP


2 Repeats a statement or group of statements while a given condition is true. It tests the
condition before executing the loop body.
PL/SQL FOR LOOP
3 Execute a sequence of statements multiple times and abbreviates the code that manages
the loop variable.

Nested loops in PL/SQL


4
You can use one or more loop inside any another basic loop, while, or for loop.

PL/SQL - Basic Loop Statement


Basic loop structure encloses sequence of statements in between
the LOOP and END LOOP statements. With each iteration, the sequence of
statements is executed and then control resumes at the top of the loop.

Syntax

The syntax of a basic loop in PL/SQL programming language is −

LOOP
Sequence of statements;
END LOOP;

Here, the sequence of statement(s) may be a single statement or a block


of statements. An EXIT statement or an EXIT WHEN statement is required to
break the loop.
PL/SQL - WHILE LOOP Statement
A WHILE LOOP statement in PL/SQL programming language repeatedly
executes a target statement as long as a given condition is true.

Syntax
WHILE condition LOOP
sequence_of_statements
END LOOP;
PL/SQL - FOR LOOP Statement
A FOR LOOP is a repetition control structure that allows you to efficiently
write a loop that needs to execute a specific number of times.

Syntax
FOR counter IN initial_value .. final_value LOOP
sequence_of_statements;
END LOOP;
Following is the flow of control in a For Loop −

 The initial step is executed first, and only once. This step allows you
to declare and initialize any loop control variables.
 Next, the condition, i.e., initial_value .. final_value is evaluated. If it
is TRUE, the body of the loop is executed. If it is FALSE, the body of
the loop does not execute and the flow of control jumps to the next
statement just after the for loop.
 After the body of the for loop executes, the value of the counter
variable is increased or decreased.
 The condition is now evaluated again. If it is TRUE, the loop executes
and the process repeats itself (body of loop, then increment step, and
then again condition). After the condition becomes FALSE, the FOR-
LOOP terminates.

Following are some special characteristics of PL/SQL for loop −

 The initial_value and final_value of the loop variable or counter can


be literals, variables, or expressions but must evaluate to numbers.
Otherwise, PL/SQL raises the predefined exception VALUE_ERROR.
 The initial_value need not be 1; however, the loop counter increment (or
decrement) must be 1.
 PL/SQL allows the determination of the loop range dynamically at run
time.
Reverse FOR LOOP Statement

By default, iteration proceeds from the initial value to the final value,
generally upward from the lower bound to the higher bound. You can
reverse this order by using the REVERSE keyword. In such case, iteration
proceeds the other way. After each iteration, the loop counter is
decremented.
PL/SQL - Nested Loops
PL/SQL allows using one loop inside another loop.

The syntax for a nested basic LOOP statement in PL/SQL is as follows −

LOOP
Sequence of statements1
LOOP
Sequence of statements2
END LOOP;
END LOOP;
The syntax for a nested FOR LOOP statement in PL/SQL is as follows −

FOR counter1 IN initial_value1 .. final_value1 LOOP


sequence_of_statements1
FOR counter2 IN initial_value2 .. final_value2 LOOP
sequence_of_statements2
END LOOP;
END LOOP;
The syntax for a nested WHILE LOOP statement in Pascal is as follows −

WHILE condition1 LOOP


sequence_of_statements1
WHILE condition2 LOOP
sequence_of_statements2
END LOOP;
END LOOP;

You might also like