----PL/SQL基本循环语句 LOOP DECLARE x number := 10; BEGIN LOOP dbms_output.put_line(x); x := x + 10; IF x > 50 THEN exit; END IF; END LOOP; -- after exit, control resumes here dbms_output.put_line('After Exit x is: ' || x); END; -- 可以用 exit when 代替 exit DECLARE x number := 10; BEGIN LOOP exit WHEN x > 50; dbms_output.put_line(x); x := x + 10; END LOOP; -- after exit, control resumes here dbms_output.put_line('After Exit x is: ' || x); END; ---- while...loop DECLARE a number(2) := 10; BEGIN WHILE a < 20 LOOP dbms_output.put_line('value of a: ' || a); a := a + 2; --增量可以随意 END LOOP; END; ----for...loop 注意:增量只能是1 DECLARE a number(2); BEGIN FOR a in 10 .. 20 LOOP dbms_output.put_line('value of a: ' || a); END LOOP; END; ---- 反转FOR LOOP语句 DECLARE a number(2) ; BEGIN FOR a IN REVERSE 10 .. 20 LOOP dbms_output.put_line('value of a: ' || a); END LOOP; END; -- 嵌套循环 求出2到100之间的素数 DECLARE i number(3); j number(3); BEGIN i := 2; LOOP j:= 2; LOOP exit WHEN ((mod(i, j) = 0) or (j = i)); j := j +1; END LOOP; IF (j = i ) THEN dbms_output.put_line(i || ' is prime'); END IF; i := i + 1; exit WHEN i = 50; END LOOP; END; -- 标记循环 DECLARE i number(1); j number(1); BEGIN << outer_loop >> FOR i IN 1..3 LOOP << inner_loop >> FOR j IN 1..3 LOOP dbms_output.put_line('i is: '|| i || ' and j is: ' || j); END loop inner_loop; END loop outer_loop; END; --循环控制语句:continue DECLARE a number(2) := 10; BEGIN -- while loop execution WHILE a < 20 LOOP dbms_output.put_line ('value of a: ' || a); a := a + 1; IF a = 15 THEN -- skip the loop using the CONTINUE statement a := a + 1; CONTINUE; -- 之后的代码跳过,回到条件开始重新迭代 END IF; END LOOP; END; -- 循环控制语句:goto 不建议使用 DECLARE a number(2) := 10; BEGIN <<loopstart>> -- while loop execution WHILE a < 20 LOOP dbms_output.put_line ('value of a: ' || a); a := a + 1; IF a = 15 THEN a := a + 1; GOTO loopstart; END IF; END LOOP; END;