PL/SQL For Loop Statement
The PL/SQL FOR LOOP
control statement will execute a block of code, when you already know how many number of times should perform or you want loop through a block of code to specific number of time.
Unlike the PL/SQL WHILE LOOP
, the number of iterations of the PL/SQL FOR LOOP
is known before the loop starts. The loop is iterated between the start and end integer values.
PL/SQL For Loop Statement Syntax
To execute a block of code, when you already know how many number of times should perform or you want loop through a block of code to specific number of time., use the following syntax:
Syntax
FOR counter IN start_value .. end_value LOOP
LOOP statements;
END LOOP;
PL/SQL For Loop Statement example
The following PL/SQL uses to execute a block of code when a given condition is true:
Example
BEGIN
FOR counter IN 1..100 LOOP
DBMS_OUTPUT.PUT_LINE(counter);
END LOOP;
END;
Let's understand how it works:
- Here, you don't need to declare the counter variable.
- Second, in each iteration, counter variable increased by 1 implicitly.
- Third, we display the result of the counter.
- Fourth, loop is terminated when the counter reaches to 100.
- Fifth, you can use EXIT WHEN statements and EXIT statements in FOR Loops.