0% found this document useful (0 votes)
29 views4 pages

MCA 3rd Sem PL-SQL Using Oracle - L5 by Anant Kumar

Go

Uploaded by

hamada.alasbahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

MCA 3rd Sem PL-SQL Using Oracle - L5 by Anant Kumar

Go

Uploaded by

hamada.alasbahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PL/SQL

USING ORACLE
CS-33

(FOR & WHILE LOOP Statement)

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.

The loop variable is self-incremental, so no explicit increment operation is needed


in this loop. The loop variable need not to be declared, as it is declared implicitly.

FOR <loop_variable> in <lower_limit> .. <higher_limit>


LOOP
<execution block starts>
.
.
.
<execution_block_ends>
END LOOP;

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.

WHILE <EXIT condition>


LOOP
<execution block starts>
.
.
.
<execution_block_ends>
END 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:
/

You might also like