수색…


통사론

  1. 고리
  2. [진술];
  3. 종료 할 때 [종료 루프 조건];
  4. 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 루프

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의 현재 값이 10보다 작을 때까지 실행됩니다.

결과:

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 루프

루프는 다른 루프와 비슷한 규칙에 따라 작동합니다. 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

루프 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


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow