MCA 3rd Sem PL-SQL Using Oracle - L5 by Anant Kumar
MCA 3rd Sem PL-SQL Using Oracle - L5 by Anant Kumar
USING ORACLE
CS-33
For
MCA 3rd Semester (Session 2018-21) Students
BY
ANANT KUMAR
MCA, M. Phil, M. Tech.
Faculty Member
Department of Computer Science
J. D. Women’s College, Patna
FOR & WHILE LOOP
FOR LOOP statement is best suitable when you want to execute a code for a
known number of times rather than based on some other conditions.
In this loop, the lower limit and the higher limit will be specified and as long as the
loop variable is in between this range, the loop will be executed.
Example:
BEGIN
dbms Qutput.put linef.Prp.gram started.' );
FOR a IN 1 .. 5
LOOP
dbms_output.put_line(a);
END LOOP:
dbms_output.put_iine('Program completed.');
END;
/
Nested Loops
The loop statements can also be nested. The outer and inner loop can be of
different types. In the nested loop, for every one iteration value of the outer loop,
the inner loop will be executed fully.
LOOP -outer
<execution block starts>
LOOP — inner
<execution_part>
END LOOP;
<execution_block_ends>
END LOOP;
DECLARE
b NUMBER;
BEGIN
dbms output put line(‘Program started' );
FOR a IN 1..3
LOOP
b:=1;
WHILE (a>=b)
LOOP
dbms output put line(a);
b:=b+1;
E D P;
E D P;
dbms_output.put_line('Program completed' );
END;
/
WHILE loop statement works similar to the Basic loop statement except the EXIT
condition is at the very beginning of the loop.
It works like an entry-check loop in which execution block will not even be
executed once if the condition is not satisfied, as the exit condition is checking
before execution part. It does not require keyword 'EXIT' explicitly to exit from the
loop since it is validating the condition implicitly each time of the loop.
DECLARE
a NUMBER :=1;
BEGIN
dbms_output.put_line('Program started');
WHILE (a <= 5)
LOOP
dbms_output.put_line(a);
a:=a+1;
END LOOP;
dbms_output.put_line(‘Program completed' );
END:
/