truncate table output
/
--------------------------------------------------------------------------------
-- Examples of all 3 types of loops
-- 1. Simple LOOP
-- 2. FOR LOOP (3 examples)
-- 3. WHILE LOOP
--------------------------------------------------------------------------------
CREATE or replace PROCEDURE loops (v_min int :=10, v_max int :=20) IS  
    i number;
--  j number;                     /* DO NOT declare interator for FOR loop */
    k number;

BEGIN
    INSERT INTO output VALUES('---Using a simple LOOP---');		
    i :=1;
    LOOP     
        insert into output 
            values('Simple loop, the line number: ' || i);
        i:=i+1;
        EXIT WHEN i > 10;
    END LOOP;


    INSERT INTO output VALUES('---Using a FOR loop with hardcoded start/end--');
    FOR j IN 1..10 LOOP
        insert into output 
            values('FOR loop1, the line number: ' || j);
    END LOOP;


    INSERT INTO output VALUES('---Using a FOR LOOP with variables start/end--'); 
    FOR  j  IN  v_min .. v_max  LOOP
        insert into output 
            values('FOR loop2, the line number: ' || j);
    END LOOP;


    INSERT INTO output VALUES('---Using a FOR LOOP in reverse order---'); 
    FOR  j  IN  REVERSE  v_min..v_max  LOOP
        insert into output 
            values('FOR loop3, the line number: ' || j);
    END LOOP;


    INSERT INTO output VALUES('---Using a WHILE loop---');
    k :=1;
    WHILE k <= 10 LOOP
        insert into output 
            values('WHILE loop, the line number: ' || k);
        k:=k+1;
    END LOOP;
END;
/

call loops()                    -- calling the procedure
/

select * from output