Loop Statements

Loop
  1. Loop is a mechanism which is supported by every programming language C, COBOL, etc.
  2. Whenever we need to execute a single or more than statements repeatedly then we use loops.
  3. Loops are categorized into two types.
Range based loops

Range based loop is a loop statements which executes statements as long as initial value reaches the final value. Once control crossed the final value then automatically comes under the loop.

E.g

For loop, for reverse.

Control based loops

Condition based loop is a loop statements which executes the statements as long as the given condition is satisfied. Once the condition is not satisfied control automatically outer the loop.

E.g

while, loop.

Note

In oracle increment/ decrement is not programmer responsibility.

Syntax

for variable in initialization . . final value
   Loop
     Statement 1;
     Statement 2;
     Statement n;

Print Waytoeasylearn 10 times on console.

declare
   i number(10);
begin
   for i in 1..10 loop
      dbms_output.put_line('Waytoeasylearn');
   end loop;
end;
For Reverse

Syntax

For variable in reverse
   Initial value .. final value loop
   Statement 1;
   Statement 2;
   End loop;

Write a PL/SQL block to print the numbers 10 to 1

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

Syntax

while(condition) loop
   Statement1;
      .
      .
   Statement n;
End loop;

Write a Pl/SQL block to print 1 to 10 number

declare
   i number:=1;
begin
   while(i<=10) loop
      dbms_output.put_line(i);
      i:=i+1;
   end loop;
end;
Loop

Syntax

Loop
   Statement 1;.
      .
   Statement n;
Exit when(condition)
End loop;

Here the statements are executed as long as the given condition is not satisfied control automatically comes outer loop.

Write a PL/SQL block to print 10 times a String.

declare
   i number:=1;
begin
   loop
      dbms_output.put_line('Waytoeasylearn');
      i:=i+1;
   exit when(i>10);
   end loop;
end;

Loop Statements
Scroll to top