Hi Everyone, Today's topic is Iterative Processing with(LOOP,WHILE LOOP,CONTINUE).
The PL/SQL loops are used to repeat the execution of one or more statements for specified number of times.
These are also known as iterative control statements.
Types of Loop in PL/SQL:
Basic Loop
While Loop
For Loop
Basic LOOP
This is a basic loop in which we and use if condition with EXIT.
Syntax:
LOOP
Sequence of statements;
END LOOP;
Loop with EXIT:
Syntax:
LOOP
statements;
EXIT;
--or EXIT WHEN condition;
END LOOP;
Example:
DECLARE
i NUMBER := 1;
BEGIN
LOOP
EXIT WHEN i>10;
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;
It will print 1 to 10 and exit.
WHILE LOOP:
PL/SQL while loop is used when a set of statements has to be executed as long as a condition is true, the While loop is used.
The condition is decided at the beginning of each iteration and continues until the condition becomes false.
Syntax of while loop:
WHILE <condition>
LOOP
statements;
END LOOP;
Example:
DECLARE
i INTEGER := 1;
BEGIN
WHILE i <= 10 LOOP
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;
It will print 1 t0 10.
FOR LOOP:
PL/SQL for loop is used when when you want to execute a set of statements for a predetermined number of times. The loop is iterated between the start and end integer values. The counter is always incremented by 1 and once the counter reaches the value of end integer, the loop ends.
Syntax:
FOR counter IN initial_value .. final_value LOOP
LOOP statements;
END LOOP;
Example:
BEGIN
FOR k IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(k);
END LOOP;
END;
It will print values 1 to 10.
Thank you for reading.
Ask your Doubt or Query in Comment Sections.