PL SQL Loops
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.
Syntax
LOOP
Sequence of statements;
END LOOP;
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.
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.
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 −