It is a basic FOR loop example program which is demonstrate how to use FOR loop in PL/SQL block. FOR loop use in PLSQL is quite differ with C program FOR loop syntax. In PLSQL you should specify the range of integer to execute the sequence of statements.
Syntax
FOR variable IN start..end
LOOP
PL/SQL block statements
END LOOP;
Sample Example to print number 1 to 10 using FOR loop.
Aim - pl/sql program to print 1 to 10 using FOR loop
Program Code: -
declare
num number(3);
begin
for num in 1..10
loop
dbms_output.put_line(num);
end loop;
end;
Output: -
1
2
3
4
5
6
7
8
9
10
Another example to print 1 to 10 in reverse order using FOR loop.
Syntax
FOR variable IN REVERSE start..end
LOOP
PL/SQL block statements
END LOOP;
Aim - program to print 1 to 10 in reverse order using FOR loop.
Program Code: -
declare
num number(3);
begin
for num in REVERSE 1..10
loop
dbms_output.put_line(num);
end loop;
end;
Output: -
10
9
8
7
6
5
4
3
2
1