开发者

using for loop how to increment any number

开发者 https://www.devze.com 2023-01-27 09:30 出处:网络
Write a program containing a loop that iterates from 1 to 1000 using a variable I, which is incremented each time around the loop. The program should output the value of I every hundred iterations (i.

Write a program containing a loop that iterates from 1 to 1000 using a variable I, which is incremented each time around the loop. The program should output the value of I every hundred iterations (i.e., the output should be 100, 200, etc). Display the output on the screen using dbms_outpu开发者_JAVA技巧t.put_line.


Use:

FOR I IN 1..1000
LOOP
   IF MOD(I, 100) = 0 THEN
     DBMS_OUTPUT.PUT_LINE(I);
   END IF;
END LOOP;

Reference:

  • FOR loop
  • IF statement
  • MOD


This might be faster:

begin
 dbms_output.put_line('100');
 dbms_output.put_line('200');
 dbms_output.put_line('300');
 dbms_output.put_line('400');
 dbms_output.put_line('500');
 dbms_output.put_line('600');
 dbms_output.put_line('700');
 dbms_output.put_line('800');
 dbms_output.put_line('900');
 dbms_output.put_line('1000');
end;

:)


Or you could implement this as

FOR I IN 1..1000 LOOP
  IF I IN (100, 200, 300, 400, 500, 600, 700, 800, 900, 1000) THEN
    DBMS_OUTPUT.PUT_LINE(I);
  END IF;
END LOOP;

or

FOR I IN 1..1000 LOOP
  IF I/100 = TRUNC(I/100) THEN
    DBMS_OUTPUT.PUT_LINE(I);
  END IF;
END LOOP;

or even

DECLARE
  INPUT_NUM   NUMBER;
  OUTPUT_NUM  NUMBER;
BEGIN
  FOR I IN 1..1000 LOOP
    SELECT I/100, TRUNC(I/100)
      INTO INPUT_NUM, OUTPUT_NUM
      FROM DUAL;

    IF INPUT_NUM = OUTPUT_NUM THEN
      DBMS_OUTPUT.PUT_LINE(I);
    END IF;
  END LOOP;
END;

Share and enjoy.

0

精彩评论

暂无评论...
验证码 换一张
取 消