A Loop in Oracle PLSQL is used to execute a portion of code i.e. the body of loop until a specific (exit) condition is not met.
In other words, we can say that we use the LOOP statement in Oracle PLSQL when we are not sure as to how many times the loop’s body should execute and we want the loop’s body to get executed at least once.
A LOOP gets terminated when it encounters an EXIT or EXIT WHEN statement which evaluates to true.
The Syntax for the LOOP Statement in Oracle PLSQL is:
LOOP
{
Statements to be executed;
}
END LOOP;
Example of a LOOP Statement in Oracle PLSQL is:
LOOP { sum := sum + 1; EXIT WHEN sum = 35; } END LOOP;
The above loop will execute the “sum := sum+1; statement 35 times. The loop will start counting from 1 and as soon as the value of the “sum” variable crosses above 35 the loop will terminate.