WHILE Loop in Oracle PLSQL

A WHILE Loop in Oracle PLSQL is used when we are not sure as to how many times the loop’s body should be executed.

Unlike LOOP statement, a WHILE LOOP may not get executed even once as the condition to execute a WHILE LOOP is evaluated before execution of the loop.

A WHILE LOOP gets terminated whenever the condition to execute the loop evaluates to false.

The Syntax for the WHILE LOOP in Oracle PLSQL is:
WHILE condition
Statements to be executed;

Example of a WHILE LOOP in Oracle PLSQL is:

[sourcecode language=”sql”]
WHILE sum <= 35
sum := sum + 1;
END LOOP;[/sourcecode]

The above loop will execute the “sum := sum+1” statement and as soon as the value of the “sum” variable crosses 35 the loop will terminate.
It may happen that the variable “sum” is already assigned a value higher than 35, in this case the WHILE LOOP will not get executed even once.

One Reply to “WHILE Loop in Oracle PLSQL”

Leave a Reply

Your email address will not be published. Required fields are marked *