FOR LOOP With REVERSE Keyword
Reverse keyword is used in FOR loop ,to iterate from end value to start value.REVERSE keyword is mentioned before the start value.
Syntax:
BEGIN
FOR loop_variable in REVERSE start_value .. end_value LOOP
set_of_statements
END LOOP;
Example: Print Number From 5 to 1 Using FOR Loop with REVERSE Keyword in PL/SQL
Query:
SET SERVEROUTPUT ON;
DECLARE
counter NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('FOR LOOP WITH REVERSE KEYWORD');
FOR counter IN REVERSE 1..5 LOOP
DBMS_OUTPUT.PUT_LINE('REVERSE VALUE: '|| counter);
END LOOP;
END;
/
Output:
Explanation:
Reverse keyword is used to iterate in reverse order.In the FOR loop ,REVERSE keyword is used to print the number from 5 to 1.Number from 5 to 1 are printed using reverse keyword in for loop.
PL/SQL For Loop
PL/SQL stands for Procedural Language/ Structured Query Language. It has block structure programming features. With PL/SQL, you can fetch data from the table, add data to the table, make decisions, perform repetitive tasks, and handle errors.PL/SQL supports SQL queries. PL/SQL contains declaration block, execution block, and exception block. Declare and exception blocks are optional.
In this article, we delve into the versatility of the PL/SQL FOR loop, a key construct for procedural programming in Oracle databases. We’ll explore its syntax, provide examples of its application, demonstrate the use of the REVERSE keyword for reverse iteration, and discuss the effectiveness of nested FOR loops.
Contact Us