Loop
Simple Loop
Section titled “Simple Loop”DECLAREv_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;WHILE Loop
Section titled “WHILE Loop”The WHILE loop is executed untill the condition of end is fulfilled. Simple example:
DECLAREv_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 declarationEND;This loop will be executed untill current value of variable v_counter will be less than ten.
The result:
Current iteration of loop is 0Current iteration of loop is 1Current iteration of loop is 2Current iteration of loop is 3Current iteration of loop is 4Current iteration of loop is 5Current iteration of loop is 6Current iteration of loop is 7Current iteration of loop is 8Current iteration of loop is 9The most important thing is, that our loop starts with ‘0’ value, so first line of results is ‘Current iteration of loop is 0’.
FOR Loop
Section titled “FOR Loop”Loop FOR works on similar rules as other loops. FOR loop is executed exact number of times and this number is known at the beginning - lower and upper limits are directly set in code. In every step in this example, loop is increment by 1.
Simple example:
DECLAREv_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 declarationEND;And the result is:
Current iteration of loop is 1Current iteration of loop is 2Current iteration of loop is 3Current iteration of loop is 4Current iteration of loop is 5Current iteration of loop is 6Current iteration of loop is 7Current iteration of loop is 8Current iteration of loop is 9Current iteration of loop is 10Loop FOR has additional property, which is working in reverse. Using additional word ‘REVERSE’ in declaration of lower and upper limit of loop allow to do that. Every execution of loop decrement value of v_counter by 1.
Example:
DECLAREv_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 declarationEND;And the result:
Current iteration of loop is 10Current iteration of loop is 9Current iteration of loop is 8Current iteration of loop is 7Current iteration of loop is 6Current iteration of loop is 5Current iteration of loop is 4Current iteration of loop is 3Current iteration of loop is 2Current iteration of loop is 1Syntax
Section titled “Syntax”- LOOP
- [statements];
- EXIT WHEN [condition for exit loop];
- END LOOP;