简体   繁体   中英

nested while loops - outer vs inner conditions

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");  

the inner loop will only be executed (tested for condition) if the outer loop is true. otherwise if outer loop breaks, nested loop has no chance to get executed.

First of all, while loop conditions are only checked at the start of each iteration.

If, at the start of an outer loop iteration, the condition is false , contol is transferred the statement following the outer loop, thereby bypassing the inner loop.

Think of code in a code block (aka the curly brackets) as a door. If you do not meet the requirments to enter the door then you cant see anything inside. So no the inner loop (since the outer loop is false) will not execute.

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).

使用局部变量控制循环,以便您可以在循环2中检查该值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM