FOR 循环中的上界在循环中不会改变,为什么?
我试图更改 For 循环中上限的值,但循环一直运行到开始时定义的上限。
根据逻辑循环应该无限,因为 v_num 的值总是比 i 早 1,但是循环执行了三次。请解释一下
这是代码
DECLARE
v_num number:=3;
BEGIN
FOR i IN 1..v_num LOOP
v_num:=v_num+1;
DBMS_OUTPUT.PUT_LINE(i ||' '||v_num);
END LOOP;
END;
Ouput Coming
1 4
2 5
3 6
I am trying to change the value of upper bound in For loop ,but the Loop is running till the upper bound which was defined in the starting.
According to logic loop should go infinite, since value of v_num is always one ahead of i,But loop is executing three time.Please explain
This is the code
DECLARE
v_num number:=3;
BEGIN
FOR i IN 1..v_num LOOP
v_num:=v_num+1;
DBMS_OUTPUT.PUT_LINE(i ||' '||v_num);
END LOOP;
END;
Ouput Coming
1 4
2 5
3 6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此行为如文档中所指定:
(Oracle 文档)
This behavior is as specified in the documentation:
(Oracle Documentation)
一般来说,FOR 循环是固定迭代
对于不确定循环,请使用 WHILE
这不是不是 Oracle 特有的,以及为什么有单独的循环结构。
Generally, FOR loops would be fixed iterations
For indeterminate looping, use WHILE
This isn't Oracle specific, and why there are separate looping constructs.
虽然更改循环变量的值通常被认为是一个坏主意,但有时这似乎是唯一的方法。但是,您可能会发现循环已优化,这可能就是这里发生的情况。
While it is generally considered a bad idea to change the loop variable's value, sometimes it seems like the only way to go. However, you might find that loops are optimized, and that might be what is happening here.
没有什么可以阻止语言设计者说“for 循环的上限仅计算一次”。这似乎是 plsql 在这里遵循的规则。
There's nothing preventing the language designers from saying "The upper bound of the for loop is evaluated only once". This appears to be the rule that plsql is following here.