We may land in a situation like to execute a block of code for several number of times. Sometimes, these statements are executed in a sequential manner. These programming languages provide us with various control structures and also allows for more complicated execution paths.
Loop statements allows us for executing a statement or group of statements multiple times and also in the general form of loop statement in most of the programming languages.
There are different types of looping statements, they are:
1.PL/SQL Basic LOOP
This looping statement encloses sequences of statements in between the LOOP and END LOOP statements. With each and every iteration, all the statements get executed and control begins at the top of the loop. EXIT OR EXIT WHEN statements are needed for breaking the loop.
Syntax:
LOOP
Sequence of statements; // may refers to the single or block of statements.
END LOOP;
Example:
DECLARE
x number := 1;
BEGIN
LOOP
dbms_output.put_line(x);
x := x + 1;
exit WHEN x > 10;
END LOOP;
-- after exit, control resumes here
dbms_output.put_line('After Exit condition x is: ' || x);
END;
/
Output:
1
2
3
4
5
After Exit condition x is: 6
PL/SQL procedure successfully completed
2.PL/SQL WHILE LOOP
This Loop statement in PL/SQL executes repeatedly at a target statement, as long as the given condition is true.
Syntax:
WHILE condition LOOP
Sequence of statements
END LOOP;
Example:
DECLARE
a number(2) := 1;
BEGIN
WHILE a < 3 LOOP
dbms_output.put_line(' a is: ' || a);
a := a + 1;
END LOOP;
END;
/
output:
a is: 1
a is: 2
PL/SQL procedure successfully completed.
3.PL/SQL FOR LOOP
This loop is said to be a repetition control structure, that allows us to efficiently write a loop and that also needs to be executed a specific number of times.
Syntax:
FOR counter IN initial value ……final volume LOOP
Sequence of statements
END LOOP;
Example:
DECLARE
x number(2);
BEGIN
FOR x in 5 .. 10 LOOP
dbms_output.put_line(' x is: ' || x);
END LOOP;
END;
/
Output:
X is: 5
X is: 6
X is: 7
X is: 8
X is: 9
X is: 10
PL/SQL procedure successfully completed
Reverse FOR LOOP Statement
In default, all the iteration proceeds from the initial value to the final value, like the lower bound and higher bound. We can reverse this order by using the reverse Keyword. Here, the iteration proceeds the other way, like after each iteration, the counter for the loop is executed.
Example:
DECLARE
x number(2) ;
BEGIN
FOR x IN REVERSE 5 .. 10 LOOP
dbms_output.put_line('x is: ' || x);
END LOOP;
END;
/
Output:
X is: 10
X is: 9
X is: 8
X is: 7
X is: 6
X is: 5
PL/SQL procedure successfully completed
Nested Loops in PL/SQL
This allows us to use one loop inside another loop. Below are some of the examples to illustrate nested loops.
Syntax for nested basic LOOP
LOOP
Sequence of statements1
LOOP
Sequence of statements2
END LOOP;
END LOOP;
Syntax for nested FOR LOOP
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;
Example:
Below is the program showing nested basic loop
DECLARE
a number(2);
b number(2);
BEGIN
a := 2;
LOOP
b:= 2;
LOOP
exit WHEN ((mod(a, b) = 0) or (b = i));
b := b +1;
END LOOP;
IF (b = a ) THEN
dbms_output.put_line(a || ' is prime');
END IF;
a := a + 1;
exit WHEN a = 10;
END LOOP;
END;
/
When we execute above program, we get following output:
2 is prime
3 is prime
5 is prime
7 is prime