PL/SQL Looping Statement


Prev Tutorial Next Tutorial

PL/SQL Looping Statement

Set of instructions given to the compiler to execute set of statements until condition becomes false is called loops. The basic purpose of loop is code repetition that means same code repeated again and again. There are three types of loops in pl-sql;

  • Simple loop
  • While loop
  • For loop

Simple loop

A Simple Loop is used when a set of statements is to be executed at least once before the loop terminates. An exit condition must be specified in the loop body, otherwise the loop will get into an infinite number of iterations. When the exit condition is satisfied the process exits from the loop body.

Syntax

variable initialize

loop
statements;
exit;
{or exit when condition; }

end loop;

Must be follow below steps;

  • Initialize a variable before the loop body.
  • Increment the variable in the loop body.
  • Use exit when statement to exit from loop body. If you use exit without when condition statement will be execute only once.

while loop

Syntax

variable initialize

while (condition)
statements;
end loop;

Must be follow below steps;

  • Initialise a variable before the loop body.
  • Increment the variable in the loop body.

for loop

A for loop is used to execute a set of statements for a predetermined number of times.

for loop

Syntax

for variable in initial_value .. final_value

loop

statements;

end loop;

Print Hello word five times

Example

begin
for i in 1 .. 4 loop
dbms_output.put_line("Hello");
end loop;
end;

Output

Hello
Hello
Hello
Hello
Hello

Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements