3-7. Iterating Until a Condition Evaluates to FALSE

Problem

You want to iterate over a series of statements until a specified condition no longer evaluates to TRUE.

Solution

Use a WHILE statement to test the condition, and execute the series of statements if the condition evaluates to TRUE; otherwise, skip the statements completely. The following example shows a WHILE statement evaluating the current value of a variable and looping through until the value of the variable reaches ten. Within the loop, this variable is being multiplied by two and printing out its current value.

DECLARE   myValue    NUMBER := 1; BEGIN WHILE myValue < 10 LOOP       DBMS_OUTPUT.PUT_LINE('The current value is: ' || myValue);       myValue := myValue * 2;   END LOOP; ...

Get Oracle and PL/SQL Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.