FOR LOOP

0
FOR LOOP:
 

  • It has the same general structure as the basic loop.
  • “FOR LOOP” contains a control statement at the front of the LOOP keyword, to determine the number of iterations that PL/SQL performs.
Syntax:
FOR loop_counter IN [REVERSE] Lowerbound.Upperbound LOOP
Statement1;
Statement2;
END LOOP;

Counter:

It is an implicitly declared INTEGER whose value is automatically increased or decreased by 1 on each iteration of the LOOP until the upper bound or lower bound is reached.

Reverse:

  • It is a keyword, and causes the counter to decrement with each iteration from the upper bound to the lower bound.
  • The loop_counter need not be declared, as it is implicitly declared as an integer.
  • The bounds of the loop are evaluated once.
  • This determines the total number of iterations that loop_counter will take on the values ranging from low_bound to high_bound, incrementing by 1 each time until the loop is complete.

SQL>DECLARE
    v_FactNum NUMBER:=&No;
    v_Factorial NUMBER:=1;
BEGIN
    FOR v_Counter IN REVERSE 1..v_FactNum
    LOOP
        v_Factorial:=v_Factorial*v_Counter;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE(‘The Factorial of’||v_FactNum||’is:’||v_Factorial);
END;

SQL>DECLARE
    j NUMBER(2):=&J;
    v VARCHAR2(100);
    k NUMBER(3);
    BEGIN
    FOR  i IN 1..10
    loop
    k:=j*i;
    v:=v||j||’*’||i||’=‘||k||’ ‘;
    end loop;
    display(v);
    END;


 Click here to go to Back page

About the author

Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus. Aenean fermentum, eget tincidunt.

0 comments:

Recent Posts