8.Loops in PLSQL (Iteratives)

PL/SQL

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

  1. Simple Loop
  2. While Loop
  3. For Loop
  4. Simple Loop: Executes statements again and again given in
    ` between loop..end loop. To stop the execution
    a condition will be given. Syntax: BEGIN statement(s); Loop statement1; statement2; .... .... Exit when Condition; END Loop; END;</code></pre></li>

Example: program to print software 10 times
declare
c number:=1;
begin
DBMS_OUTPUT.PUT_LINE (‘HARDWARE’);
loop
dbms_output.put_line (‘Software’);
C:=C+1;
EXIT WHEN C>10;
end loop;
DBMS_OUTPUT.PUT_LINE (‘COMPUTER’);
end;

Output

HARDWARE
software
software
software
software
software
software
software
software
software
software
COMPUTER

============================================
Example: Print 2 to 100 even numbers 2,4,6,8,10,12….100
Example: Print numbers from 10 to 1 (10,9,8….1)
Example: Print 1 to 99 odd numbers (1,3,5,7…)
Example: Print a mathematical table (21=2, 22=4, 2*3=6)