嵌套while循环

编程入门 行业动态 更新时间:2024-10-15 22:30:25
嵌套while循环 - 外部与内部条件(nested while loops - outer vs inner conditions)

在下面的例子中,在外部循环条件变为false之后,内部while循环是否继续执行它的语句2? 或者,一旦外部while循环条件变为false,即使内部while循环的条件为真,ENTIRE循环也会退出,包括内部while循环?

while (becomes false){ statements 1; statements 1; while ( true at the time the outer loop condition become false){ statements 2; statements 2; } } println("print something interesting");

In the example below does the inner while loop continue to execute it's statements 2 after the outer loop condition becomes false? Or once the outer while loop condition becomes false the ENTIRE loop exits including the inner while loop even though the condition for the inner while loop is true?

while (becomes false){ statements 1; statements 1; while ( true at the time the outer loop condition become false){ statements 2; statements 2; } } println("print something interesting");

最满意答案

了解while循环的工作原理可能会有所帮助。 如果你写:

while(myCondition) { doSomething(); doSomethingElse(); } println("print something interesting");

然后它基本上变成(伪代码):

1. if myCondition continue to line 2, else go to line 5 2. doSomething() 3. doSomethingElse() 4. go to line 1 5. println "print something interesting"

如您所见,仅在第1行检查条件。例如,如果myCondition在第2行中变为false,则没有任何内容可以使块短路。 (如果你需要它,你必须手动重新测试myCondition并且如果它是假的则调用break 。)即使doSomething()被while循环, for循环或其他任何东西替换也是如此。

因此,如果“语句2”使外部循环条件为假,则不会停止任何操作。 程序将继续它的快乐方式,直到内部循环结束,此时out循环完成其迭代(因为在外部循环块内的内部循环之后没有任何内容)并返回重新测试其条件以查看它是否应该开始另一次迭代。 只有这样外循环才能检查其状态。

为了使它更明确,以下是您的代码基本上如何工作:

1. if outer-condition continue to line 2, else go to line 9 2. statements-1 3. statements-2 4. if inner-condition continue to line 5, else go to line 8 5. statements-2 6. statements-2 7. go to line 4 8. go to line 1 9. println "print something interesting"

因此,外部条件在第1行检查。如果它在任何点变为假,则循环甚至不会注意到它到达第1行,这只会在它到达第8行(或第一次)时发生点击第1行开始while循环,当然)。

It may help to understand how a while loop works. If you write:

while(myCondition) { doSomething(); doSomethingElse(); } println("print something interesting");

Then it basically becomes (in pseudo-code):

1. if myCondition continue to line 2, else go to line 5 2. doSomething() 3. doSomethingElse() 4. go to line 1 5. println "print something interesting"

As you can see, the condition is only checked at line 1. If myCondition becomes false during line 2, for instance, there's nothing to short-circuit the block. (If you need that, you have to manually re-test myCondition and call break if it's false.) This is true even if doSomething() is replaced by a while loop, for loop, or anything else.

So, if "statements 2" make the outer loop condition false, nothing will stop. The program will go on its merry way until the inner loop finishes, at which point the out loop finishes its iteration (since there's nothing after the inner loop within the outer loop's block) and goes back to re-test its condition to see if it should start another iteration. Only then does the outer loop check its condition.

To make it a tad more explicit, here's how your code basically works:

1. if outer-condition continue to line 2, else go to line 9 2. statements-1 3. statements-2 4. if inner-condition continue to line 5, else go to line 8 5. statements-2 6. statements-2 7. go to line 4 8. go to line 1 9. println "print something interesting"

So, outer-condition is only checked at line 1. If it becomes false at any point, the loop won't even notice until it gets to line 1, which will only happen once it gets to line 8 (or the first time it hits line 1 to start the while loop, of course).

更多推荐

本文发布于:2023-08-06 19:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455815.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!