Zoeken…
Syntaxis
- LUS
- [Statements];
- EXIT WHEN [voorwaarde voor exit-lus];
- EINDE LOOP;
Eenvoudige lus
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;
Herhalingslus
De WHILE-lus wordt uitgevoerd totdat aan de voorwaarde einde is voldaan. Eenvoudig voorbeeld:
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;
Deze lus wordt uitgevoerd totdat de huidige waarde van variabele v_counter kleiner is dan tien.
Het resultaat:
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
Het belangrijkste is dat onze lus begint met de waarde '0', dus de eerste regel met resultaten is 'Huidige iteratie van lus is 0'.
For loop
Loop FOR werkt op dezelfde regels als andere loops. De FOR-lus wordt exact het aantal keren uitgevoerd en dit nummer is in het begin bekend - de onder- en bovengrenzen worden rechtstreeks in code ingesteld. In elke stap in dit voorbeeld wordt de lus met 1 verhoogd.
Eenvoudig voorbeeld:
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;
En het resultaat is:
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 heeft extra eigenschappen, die omgekeerd werken. Gebruik extra woord 'ACHTERUIT' bij het aangeven van de onder- en bovengrens van de lus. Elke uitvoering van de lusverminderingswaarde van v_counter met 1.
Voorbeeld:
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;
En het resultaat:
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