FOR loop with hops in PLSQL

0

I have to do an exercise that is a for loop but delimiting the minimum the maximum and the jump, the thing is that both the minimum and the maximum is done but the jump is not how to define it, I hope someone helps me.

I leave you my exercise without the defined jump:

 SET SERVEROUTPUT ON
 CREATE OR REPLACE PROCEDURE CONTAR(var1 NUMBER)
 IS
 contador NUMBER;
 BEGIN
 contador:=0;
 FOR numero IN contador..var1
   LOOP
   DBMS_OUTPUT.PUT_LINE('Numero:' || contador);
   contador:=contador+1;
 END LOOP;
 END;

 SET SERVEROUTPUT ON
 BEGIN
 CONTAR(10);
 END;

Greetings!

    
asked by okaiso 08.05.2018 в 11:31
source

2 answers

1

If by jump you mean that for example instead of telling you normal follow some established pattern (count 2 in 2, 3 in 3, only pairs, odd, etc), simply integrate an IF into the FOR with that pattern. For example, here you are only going to count the even numbers:

SET SERVEROUTPUT ON
 CREATE OR REPLACE PROCEDURE CONTAR(var1 NUMBER)
 IS
 contador NUMBER;
 BEGIN
 contador:=0;
 FOR numero IN contador..var1
 LOOP
   IF MOD(numero,2)=0 THEN
       DBMS_OUTPUT.PUT_LINE('Numero:' || contador);
       contador:=contador+1;
   END IF
 END LOOP;
 END;

 SET SERVEROUTPUT ON
 BEGIN
 CONTAR(10);
 END;

If you need more details, we would need you to specify what type of jump you need.

    
answered by 08.05.2018 в 12:56
0

Good morning okasio, if what you need is to advance your loop in a different order than i ++, you can use a while, I'll give you an example:

DECLARE
   c_loop BINARY_INTEGER;
BEGIN
   c_loop := a;
   LOOP
      EXIT WHEN c_loop > b;
      /*TU PROCESO*/
      c_loop := c_loop + c;
   END LOOP;
END;

Where a = Your minimum, b = your maximum, and c = your jump. That is, if you have a = 0, b = 10, c = 2. Then you will be looping from 0 to 10 from 2 to 2, and the c_loop values will be 0, 2, 4, 6, 8, 10.

    
answered by 11.07.2018 в 22:20