× PL/SQL - Overview PL/SQL - Basic Syntax PL/SQL - Data Types PL/SQL - Variables PL/SQL - Constants and Literals PL/SQL - Operators PL/SQL - Conditions PL/SQL -ifelse PL/SQL -elsif PL/SQL -nestedif PL/SQL -Case PL/SQL -Searched Case PL/SQL -Basic Loop PL/SQL -For Loop PL/SQL -While Loop PL/SQL - Strings PL/SQL - Arrays PL/SQL - Procedures PL/SQL - Functions PL/SQL - Cursors PL/SQL - Records PL/SQL - Exceptions PL/SQL - Triggers PL/SQL - Packages PL/SQL - Collections PL/SQL - Transactions PL/SQL - Date & Time PL/SQL - DBMS Output PL/SQL - Object Oriented
  • iconPLSQL Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

PL/SQL - Basic Loop Statement



PL/SQL - Basic Loop Statement
Basic loop structure encloses sequence of statements in between the LOOP and END LOOP statements. With each iteration, the sequence of statements is executed and then control resumes at the top of the loop.
Syntax
The syntax of a basic loop in PL/SQL programming language is −
LOOP 
   Sequence of statements; 
END LOOP; 

Here, the sequence of statement(s) may be a single statement or a block of statements. An EXIT statement or an EXIT WHEN statement is required to break the loop.
Example
Dispaly 1-10 Numbers Numbers
DECLARE 
   x number := 1; 
BEGIN 
   LOOP 
      dbms_output.put_line(x); 
      x := x + 1; 
      IF x > 10 THEN 
         exit; 
      END IF; 
   END LOOP; 
   -- after exit, control resumes here  
   dbms_output.put_line('After Exit x is: ' || x); 
END; 
/

When the above code is executed at the SQL prompt, it produces the following result −
1
2
3
4
5
6
7
8
9
10
After Exit x is: 11

PL/SQL procedure successfully completed.

You can use the EXIT WHEN statement instead of the EXIT statement −
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 x is: ' || x); 
END; 
/

When the above code is executed at the SQL prompt, it produces the following result −
1
2
3
4
5
6
7
8
9
10
After Exit x is: 11

PL/SQL procedure successfully completed.

Dispaly 10-1 Numbers Numbers
    
declare 
x int:=10;
begin
loop

dbms_output.put_line(x);
if(x<=0) then
exit;
end if;
x:=x-1;

end loop;
end;
/

Dispaly Even Numbers Numbers
declare 
x int:=10;
begin
loop

if(x mod 2=0)then
dbms_output.put_line(x);
end if;

if(x<=0) then
exit;
end if;
x:=x-1;

end loop;
end;
/



Nested Basic Loop
One basic loop with in another basic loop
Syntax
loop
	loop
	...
	...
	end loop;
...
...
end loop;
in the above syntax
outerloop row
innerloop column
Example #1
***
***
***
***
***
***

declare 
r int;
c int;

begin
r:=1;
loop
	c:=1;
	loop
	dbms_output.put('*');
	if(c>=3) then 
	exit;
	end if;
	c:=c+1;
	

	end loop;

dbms_output.put_line('');
if(r>5) then
exit;
end if;
r:=r+1;
end loop;
end;
/

Example #2
*
**
***
****
*****
******

declare 
r int;
c int;

begin
r:=1;
loop
	c:=1;
	loop
	dbms_output.put('*');
	if(c>=r) then 
	exit;
	end if;
	c:=c+1;
	end loop;

dbms_output.put_line('');
if(r>5) then
exit;
end if;
r:=r+1;
end loop;
end;
/

Example #3

******
*****
****
***
**
*

declare 
r int;
c int;

begin
r:=1;
loop
	c:=6;
	loop
	dbms_output.put('*');
	if(c<=r) then 
	exit;
	end if;
	c:=c-1;
	end loop;

dbms_output.put_line('');
if(r>5) then
exit;
end if;
r:=r+1;
end loop;
end;
/


Example #4
*******
*     *
*     *
*     *
*     *
*     *
*******


declare 
r int;
c int;

begin
r:=1;
loop
	c:=7;
	loop
	if(r=1 or r=7 or c=1 or c=7) then
	dbms_output.put('*');
	else
	dbms_output.put(' ');
	end if;
	if(c<=1) then 
	exit;
	end if;
	c:=c-1;
	end loop;

dbms_output.put_line('');
if(r>=7) then
exit;
end if;
r:=r+1;
end loop;
end;
/

Example #5
   *   
   *   
   *   
*******
   *   
   *   
   *   


declare 
r int;
c int;

begin
r:=1;
loop
	c:=7;
	loop
	if(r=4 or c=4) then
	dbms_output.put('*');
	else
	dbms_output.put(' ');
	end if;
	if(c<=1) then 
	exit;
	end if;
	c:=c-1;
	end loop;

dbms_output.put_line('');
if(r>=7) then
exit;
end if;
r:=r+1;
end loop;
end;
/

Example #6

      *
     **
    ***
   ****
  *****
 ******
*******



declare 
r int;
c int;

begin
r:=1;
loop
	c:=7;
	loop
	if(r>=c) then
	dbms_output.put('*');
	else
	dbms_output.put(' ');
	end if;
	if(c<=1) then 
	exit;
	end if;
	c:=c-1;
	end loop;

dbms_output.put_line('');
if(r>=7) then
exit;
end if;
r:=r+1;
end loop;
end;
/

Key Points

  • PL/SQL - Basic Loop Statement
  • Image