10.Loop for Loop

PL/SQL

Execute statements repeatedly is called loop/iterative statement.
Loops are 3 types

  1. Simple Loop
  2. While Loop
  3. For Loop

Using For Loop:
syntax: BEGIN
FOR var in ..
LOOP
statement(s);
END Loop;

Example: Program to print 1-10 numbers

declare
c number;
begin
for c in 1..10
loop
dbms_output.put_line (c);
end loop;
end;

Example: Program to print from 10 to 1

declare
c number;
begin
for c in reverse 1..10
loop
dbms_output.put_line (c);
end loop;
end;

In For no intial value is required in declare section for the variable
No increment statement is [email protected] like c=c+1
No condition required like c>=10

Example: Print a mathematical table

declare
n number:=5;
m number:=6
c number;
p number;
begin
for c in 1..10
loop
p:=nc; dbms_output.put_line (n||’‘||c||’=’||p);
end loop;
end;
/

/* Output

81=8 82=16

8*3=24


8*10=80
*/