× 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 - For Loop Statement



PL/SQL - For Loop Statement
A FOR LOOP is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax
FOR counter IN initial_value .. final_value LOOP 
   sequence_of_statements; 
END LOOP;
Following is the flow of control in a For Loop
1.The initial step is executed first, and only once. This step allows you to declare and initialize any loop control variables.
2.Next, the condition, i.e., initial_value .. final_value is evaluated. If it is TRUE, the body of the loop is executed. If it is FALSE, the body of the loop does not execute and the flow of control jumps to the next statement just after the for loop.
3.After the body of the for loop executes, the value of the counter variable is increased or decreased.
4.The condition is now evaluated again. If it is TRUE, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes FALSE, the FOR-LOOP terminates.


Following are some special characteristics of PL/SQL for loop
1.The initial_value and final_value of the loop variable or counter can be literals, variables, or expressions but must evaluate to numbers. Otherwise, PL/SQL raises the predefined exception VALUE_ERROR.
2.The initial_value need not be 1; however, the loop counter increment (or decrement) must be 1.
3.PL/SQL allows the determination of the loop range dynamically at run time.

Example Program Display 1-10 Numbers
DECLARE 
   a int; 
BEGIN 
   FOR a in 1 .. 10 LOOP 
      dbms_output.put_line('value of a: ' || a); 
  END LOOP; 
END; 
/
Output:
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
value of a: 10

Reverse FOR LOOP Statement
By default, iteration proceeds from the initial value to the final value, generally upward from the lower bound to the higher bound. You can reverse this order by using the REVERSE keyword. In such case, iteration proceeds the other way. After each iteration, the loop counter is decremented.
However, you must write the range bounds in ascending (not descending) order. The following program illustrates this

DECLARE 
   a int ; 
BEGIN 
   FOR a IN REVERSE 10 .. 1 LOOP 
      dbms_output.put_line('value of a: ' || a); 
   END LOOP; 
END; 
/
Output:
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 For Loop
One basic loop with in another basic loop
Syntax
FOR counter IN initial_value .. final_value LOOP 
    FOR counter IN initial_value .. final_value LOOP 
                sequence_of_statements; 
    END LOOP;
   sequence_of_statements; 
END LOOP;
in the above syntax
outerloop row
innerloop column
Example #1
***
***
***
***
***
***

declare
r int;
c int;

begin
 for r in 1..6 loop
	for c in 1..3 loop
	dbms_output.put('*');
	end loop;
 dbms_output.put_line('');
 end loop;

end;
/



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

declare
r int;
c int;

begin
 for r in 1..6 loop
	for c in 1..r loop
	dbms_output.put('*');
	end loop;
 dbms_output.put_line('');
 end loop;

end;
/

Example #3

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

declare
r int;
c int;

begin
 for r in 1..6 loop
	for c in reverse r..6 loop
	dbms_output.put('*');
	end loop;
 dbms_output.put_line('');
 end loop;

end;
/


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


declare
r int;
c int;

begin
 for r in 1..7 loop
	for c in reverse 1..7 loop
		if(r=1 or c=1 or r=7 or c=7) then 
			dbms_output.put('*');
		else
			dbms_output.put(' ');
		end if;
	end loop;
 dbms_output.put_line('');
 end loop;

end;
/

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


declare
r int;
c int;

begin
 for r in 1..7 loop
	for c in reverse 1..7 loop
		if(r=4  or c=4) then 
			dbms_output.put('*');
		else
			dbms_output.put(' ');
		end if;
	end loop;
 dbms_output.put_line('');
 end loop;

end;
/



Example #6

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



declare
r int;
c int;

begin
 for r in 1..7 loop
	for c in reverse 1..7 loop
		if(r>=c) then 
			dbms_output.put('*');
		else
			dbms_output.put(' ');
		end if;
	end loop;
 dbms_output.put_line('');
 end loop;

end;
/

Key Points

  • PL/SQL - For Loop Statement
  • Image