Buscar..


Sintaxis

  1. LAZO
  2. [declaraciones];
  3. SALIR CUANDO [condición para el bucle de salida];
  4. Bucle final;

Bucle simple

DECLARE
v_counter NUMBER(2);

BEGIN
  v_counter := 0;
  LOOP
    v_counter := v_counter + 1;
    dbms_output.put_line('Line number' || v_counter);
    
    EXIT WHEN v_counter = 10;
  END LOOP;
END;

Mientras que bucle

El bucle WHILE se ejecuta hasta que se cumpla la condición de fin. Ejemplo simple:

DECLARE
v_counter NUMBER(2); --declaration of counter variable

BEGIN
  v_counter := 0; --point of start, first value of our iteration
 
  WHILE v_counter < 10 LOOP --exit condition
    
    dbms_output.put_line('Current iteration of loop is ' || v_counter); --show current iteration number in dbms script output
    v_counter := v_counter + 1; --incrementation of counter value, very important step

  END LOOP; --end of loop declaration
END;

Este bucle se ejecutará hasta que el valor actual de la variable v_counter sea menor que diez.

El resultado:

Current iteration of loop is 0
Current iteration of loop is 1
Current iteration of loop is 2
Current iteration of loop is 3
Current iteration of loop is 4
Current iteration of loop is 5
Current iteration of loop is 6
Current iteration of loop is 7
Current iteration of loop is 8
Current iteration of loop is 9

Lo más importante es que nuestro bucle comienza con el valor '0', por lo que la primera línea de resultados es 'La iteración actual del bucle es 0'.

En bucle

Loop FOR trabaja en reglas similares a las de otros bucles. El bucle FOR se ejecuta el número exacto de veces y este número se conoce al principio; los límites inferior y superior se establecen directamente en el código. En cada paso de este ejemplo, el bucle se incrementa en 1.

Ejemplo simple:

DECLARE
v_counter NUMBER(2); --declaration of counter variable

BEGIN
  v_counter := 0; --point of start, first value of our iteration, execute of variable
 
  FOR v_counter IN 1..10 LOOP --The point, where lower and upper point of loop statement is declared - in this example, loop will be executed 10 times, start with value of 1
    
    dbms_output.put_line('Current iteration of loop is ' || v_counter); --show current iteration number in dbms script output
   
  END LOOP; --end of loop declaration
END;

Y el resultado es:

Current iteration of loop is 1
Current iteration of loop is 2
Current iteration of loop is 3
Current iteration of loop is 4
Current iteration of loop is 5
Current iteration of loop is 6
Current iteration of loop is 7
Current iteration of loop is 8
Current iteration of loop is 9
Current iteration of loop is 10

Loop FOR tiene propiedad adicional, que está funcionando a la inversa. El uso de la palabra adicional 'REVERSE' en la declaración del límite inferior y superior del bucle permite hacer eso. Cada ejecución del valor de decremento de bucle de v_counter por 1.

Ejemplo:

DECLARE
v_counter NUMBER(2); --declaration of counter variable

BEGIN
  v_counter := 0; --point of start
 
  FOR v_counter IN REVERSE 1..10 LOOP
    
    dbms_output.put_line('Current iteration of loop is ' || v_counter); --show current iteration number in dbms script output
   
  END LOOP; --end of loop declaration
END;

Y el resultado:

Current iteration of loop is 10
Current iteration of loop is 9
Current iteration of loop is 8
Current iteration of loop is 7
Current iteration of loop is 6
Current iteration of loop is 5
Current iteration of loop is 4
Current iteration of loop is 3
Current iteration of loop is 2
Current iteration of loop is 1


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow