3-12. Stepping Through a Loop Based on Odd-Numbered Increments
Problem
Rather than iterating through a range of numbers by even increments, you prefer to loop through the range using odd increments.
Solution
Use the built-in MOD
function to determine whether the current index is odd. If it is odd, then print out the value; otherwise, continue to the next iteration. The following example shows how to implement this solution:
BEGIN
FOR idx IN 1..10 LOOP
IF MOD(idx,2) != 0 THEN
DBMS_OUTPUT.PUT_LINE('The current index is: ' || idx);
END IF;
END LOOP;
END;
Results:
The current index is: 1
The current index is: 3
The current index is: 5
The current index is: 7
The current index is: 9
PL/SQL procedure successfully completed.
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.