24 Jan 2013

Reversing the loop: REVERSE

Demonstrate the use of REVERSE keyword in a LOOP.

create or replace PROCEDURE SP_REVERSE_LOOP AS /********************************************************************* NAME: SP_REVERSE_LOOP PURPOSE: An example to reverse a looping. *********************************************************************/ la_nums  DBMS_SQL.VARCHAR2A; BEGIN FOR i IN 1..10 LOOP la_nums(i) := i * i; END LOOP; FOR i IN 1..10 LOOP dbms_output.put_line(i || ': ' || la_nums(i)); END LOOP; dbms_output.put_line('******************'); FOR i IN 10..1 LOOP dbms_output.put_line(i || ': ' || la_nums(i)); END LOOP; dbms_output.put_line('******************'); FOR i IN REVERSE 1..10 LOOP dbms_output.put_line(i || ': ' || la_nums(i)); END LOOP; dbms_output.put_line('******************'); END;

Output

1: 1 2: 4 3: 9 4: 16 5: 25 6: 36 7: 49 8: 64 9: 81 10: 100 ****************** ****************** 10: 100 9: 81 8: 64 7: 49 6: 36 5: 25 4: 16 3: 9 2: 4 1: 1 ******************

No comments: