PL/SQL For Loop

In ‘FOR Loop’ index variable internally behaves like a integer datatype variable, whereas in ‘cursor for loops’ index variable internally behaves like a ‘Record Type Variable’ (%ROWTYPE)

Syntax of FOR Loop:

FOR IndexVariableName IN LowerBound … UpperBound

LOOP

Executable Statements;

END LOOP;

Note: We are not allow to use increment or decrement in ‘For Loops’

Write a program to print number into increasing manner, syntax shown below

DECLARE

i number(10);

BEGIN

For i in 1..10

LOOP

Dbms_output.put_line(i);

END LOOP;

END;

Write a program to print number into decreasing manner

DECLARE

i number(10);

BEGIN

For i in REVERSE 1..10

LOOP

Dbms_output.put_line(i);

END LOOP;

END;

In ‘FOR LOOP’ index variable internally behaves like a integer in ‘FOR LOOP’ declare section is optional

BEGIN

For i in 1..10

LOOP

Dbms_output.put_line(i);

END LOOP;

END;