Поиск…
Синтаксис
- LOOP
- [заявления];
- EXIT WHEN [условие для цикла выхода];
- END LOOP;
Простая петля
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;
WHILE Loop
Цикл WHILE выполняется до тех пор, пока не будет выполнено условие конца. Простой пример:
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;
Этот цикл будет выполнен до тех пор, пока текущее значение переменной v_counter не будет меньше десяти.
Результат:
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
Самое главное, что наш цикл начинается с значения «0», поэтому первая строка результатов «Текущая итерация цикла равна 0».
FOR Loop
Loop FOR работает по аналогичным правилам, как и другие циклы. FOR цикл выполняется точное число раз, и это число известно в начале - нижний и верхний пределы напрямую задаются в коде. На каждом шаге этого примера цикл увеличивается на 1.
Простой пример:
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;
И результат:
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 имеет дополнительное свойство, которое работает в обратном порядке. Использование дополнительного слова «REVERSE» в объявлении нижнего и верхнего пределов цикла позволяет это сделать. Каждое выполнение значения декремента цикла v_counter на 1.
Пример:
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;
И результат:
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