for in loop plsql

Pl sql for in loop:

The pl sql for in loop repeatedly executes a block of statements for a fixed number of times. The loop iteration occurs between the start and end integer values. The counter is always incremented by 1 and loop terminates when the counter reaches the value of the end integer.

Pl sql for in loop syntax:

FOR loop_counter IN [REVERSE] start_value .. end_value
LOOP
   //block of statements.
END LOOP;

Note: 1. The double dot (..) specifies the range operator. 2. By default iteration is from start_value to end_value but we can reverse the iteration process by using REVERSE keyword. 3. No need to declare the counter variable explicitly because it is declared implicitly in the declaration section. 4. The counter variable is incremented by 1 and does not need to be incremented explicitly. 5. The EXIT and EXIT WHEN statements can be used.

Pl sql for in loop example:

DECLARE  
BEGIN  
  FOR var IN 1..10
  LOOP    
    DBMS_OUTPUT.PUT_LINE(var);   
  END LOOP;  
END;

Output:

1
2
3
4
5
6
7
8
9
10

Pl sql for in loop reverse example:

DECLARE  
BEGIN  
  FOR var IN REVERSE 1..10
  LOOP    
    DBMS_OUTPUT.PUT_LINE(var);   
  END LOOP;  
END;
 
Output:
10
9
8
7
6
5
4
3
2
1

 

Please follow and like us:
Content Protection by DMCA.com