× 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 - WHILE LOOP Statement


PL/SQL - WHILE LOOP Statement
A WHILE LOOP statement in PL/SQL programming language repeatedly executes a target statement as long as a given condition is true.
Syntax
WHILE condition LOOP 
   sequence_of_statements 
END LOOP; 
Example Program Display 1-10 numbers
DECLARE 
   a number(2) := 1; 
BEGIN 
   WHILE a <= 10 LOOP 
      dbms_output.put_line('value of a: ' || a); 
      a := a + 1; 
   END LOOP; 
END; 
/
When the above code is executed at the SQL prompt, it produces the following result −
value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 5
value of a: 6
value of a: 7
value of a: 8
value of a: 9

Example Program Display 10-1 numbers
DECLARE 
   a number(2) := 10; 
BEGIN 
   WHILE a >= 1 LOOP 
      dbms_output.put_line('value of a: ' || a); 
      a := a - 1; 
   END LOOP; 
END; 
/
When the above code is executed at the SQL prompt, it produces the following result −

value of a: 10
value of a: 9
value of a: 8
value of a: 7
value of a: 6
value of a: 5
value of a: 4
value of a: 3
value of a: 2
value of a: 1

Nested While Loop
One while loop with in another while
while(condition) loop

	while(condition) loop
	...
	..
	end loop;
..
..
end loop;


Program -1
***
***
***
***
***
***

declare
r int;
c int; 
begin
r:=1;
while(r<=6) loop
	c:=1;
	while(c<=3) loop
	dbms_output.put('*');
	c:=c+1;	
	end loop;

dbms_output.put_line('');
r:=r+1;

end loop;

end;
/
Program -2

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



declare
r int;
c int; 
begin
r:=1;
while(r<=6) loop
	c:=1;
	while(c<=r) loop
	dbms_output.put('*');
	c:=c+1;	
	end loop;

dbms_output.put_line('');
r:=r+1;

end loop;

end;
/

Program -3

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

declare
r int;
c int; 
begin
r:=1;
while(r<=6) loop
	c:=6;
	while(c>=r) loop
	dbms_output.put('*');
	c:=c-1;	
	end loop;

dbms_output.put_line('');
r:=r+1;

end loop;

end;
/

Program -4
*******
*     *
*     *
*     *
*     *
*     *
*******

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

dbms_output.put_line('');
r:=r+1;

end loop;

end;
/


Program -5

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



Program -6
declare
r int;
c int; 
begin
r:=1;
while(r<=7) loop
	c:=7;
	while(c>=1) loop
	if(r=4 or c=4 ) then
	dbms_output.put('*');
	else
	dbms_output.put(' ');
	end if;
	c:=c-1;	
	end loop;

dbms_output.put_line('');
r:=r+1;

end loop;

end;
/


Program -7


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

dbms_output.put_line('');
r:=r+1;

end loop;

end;
/

Key Points

  • PL/SQL - WHILE LOOP Statement
  • Image