9.Loop While Loop

PL/SQL

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

  1. Simple Loop
  2. While Loop
  3. For Loop
  4. While Loop: While is used to check the condition first and enter into the loop to
    executes statements again and again given in
    ` between loop..end loop.. Syntax: BEGIN statement(s); while <condition> Loop statement1; statement2; .... .... END Loop; statement(s); END; All the statements are repeated given in loop...end loop until the condition is false.

Example: Develop a program to print 1-10 numbers

DECLARE
c number:=1;
BEGIN
while c<=10
loop
dbms_output.put_line (c);
c:=c+1;
end loop;
END;

Output

1
2
3
4
.
.
10

Example: Develop a program to print 10-1 numbers

DECLARE
c number:=10;
BEGIN
while c>=1
loop
dbms_output.put_line (c);
c:=c-1;
end loop;
END;

10
9
8
7
.
.
.
.
1