在嵌套循环中设置 “break” 的位置。

huangapple go评论64阅读模式
英文:

where to set "break" in nested loop

问题

code 1: 在循环 j 的第一轮之后(j=0),中断了循环,但继续执行嵌套循环本身 - 这段代码对我来说是可理解的,用作与下一个代码的比较。

code 2: 在第一轮(i=0,j=0,1,2)之后中断了整个嵌套循环,为什么它不只中断 j?与代码 1 相比,我认为 break 已经从循环 j 的单轮移动到了整个循环 j。但事实并非如此。我已经阅读过,break 中断最近的循环,这应该是 j。

code 3: 在这里,我甚至将 break 移得更远,但得到了与代码 2 相同的结果。

英文:

I am learning about nested loops and I have come across the use of break. I don't quite understand where to place break in the nested loop to get the correct output.
I have tried three different codes, and don't understand them fully.

code 1: Interruption of the loop after the first round of j (j=0), but continuation of the nested loop itself - this code is understandable for me and is used as comparison for the next codes.

for i in range(6):                 
    print('hello')
    if i<=3:                        
        for j in range(3):
            print('hi')             
            break                   
        print('bye')                  

code 2: interrupts the entire nested loop after the first round (i=0, j=0,1,2), why doesn't it just interrupt j? Compared to code 1, I thought break has moved from the single round of loop j to the whole loop of j. But this does not seem to be the case. And I have read, break interruppts the closest loop, which would be j.

for i in range(6):
    print('hello')
    if i<=3:
        for j in range(3):
            print('hi')
        break                       
        print('bye')  

code 3: And here I moved break even further, but get the same result as code 2.

for i in range(6):
    print('hello')
    if i<=3:
        for j in range(3):
            print('hi')
    break                      
    print('bye')

答案1

得分: 0

break会跳出直接包含它的最内部循环。

for i in range(...):
   ...
   break

这个breakfor i循环内部,所以它将停止整个for i循环。任何外部的循环都不受影响:

for j in ...:
   for i in ...:
      break # 终止`for i`循环
   # `for j`循环继续

break放在另一个循环的旁边会影响包含它的循环,无论如何:

for i in ...:
    for j in ...:
        print('Hello')
    break
    # 在`for j`外部,但在`for i`内部,
    # 所以只有`for i`循环受到此`break`的影响

"break中断最近的循环"意味着"break中断最内部的循环"。

break放在一个if语句内部仍然会终止包含它的循环,但只有在满足某些条件时才会发生:

for i in ...:
    for j in ...:
       if i > j: break # 仅当`i > j`时才退出`for j`循环
英文:

break breaks out of the innermost loop the break statement is directly inside of.

for i in range(...):
   ...
   break

This break is inside the for i loop, so it'll stop the entire for i loop. Any loops that are outside are untouched:

for j in ...:
   for i in ...:
      break # Terminate the `for i` loop
   # The `for j` loop continues

Placing break next to another loop affects the loop break is inside of anyway:

for i in ...:
    for j in ...:
        print('Hello')
    break
    # Outside `for j`, but inside `for i`,
    # so only `for i` loop can be affected by this `break`

"break interrupts the closest loop" means "break interrupts the innermost loop".

Placing break inside an if statement still terminates the loop the break is inside of, but only if some condition is met:

for i in ...:
    for j in ...:
       if i > j: break # Get out of the `for j` loop ONLY IF `i > j`

答案2

得分: 0

你理解第一个代码块的流程和输出,这很棒。如果你不理解其他代码块的流程,你可以尝试使用调试器逐行执行代码,以查看它是如何工作的。

现在让我们看看第二个代码块:

for i in range(6):
    print('hello')
    if i<=3:
        for j in range(3):
            print('hi')
        break                       
        print('bye')

我们期望这段代码会打印一次 'hello',因为第一次打印是当 i0 时。由于 0 小于或等于 3,我们进入了 if 块并继续打印 'hi' 三次,然后我们遇到了 break 语句,这使我们跳出了 for i 循环,因为 break 作用于我们所在的循环上下文,而不是地理上最接近的循环。请注意,我们永远不会执行 print('bye'),因为 break 将我们跳出了循环。

现在是第三个代码块:

for i in range(6):
    print('hello')
    if i<=3:
        for j in range(3):
            print('hi')
    break                      
    print('bye')

我们得到相同的输出,“hello”和 3 次“hi”,但 break 的发生时间略有不同。请注意,我们仍然永远不会执行 print('bye')

在这第三个代码块中,与其在我们的 if 语句内部执行 break 作为最后一件事,现在我们在 if 语句之后立即执行 break

英文:

You understand the code flow and output of the first block of code and that is great. If you don't follow the flow in the other blocks, you might be able to use a debugger to execute the code line by line to see how it works.

That aside, let's look at block 2:

for i in range(6):
    print(&#39;hello&#39;)
    if i&lt;=3:
        for j in range(3):
            print(&#39;hi&#39;)
        break                       
        print(&#39;bye&#39;)  

We would expect this code to print(&#39;hello&#39;) one time as the first time we print is when i is 0. Since 0 is less than or equal to 3, we enter the if block and go on to print(&#39;hi&#39;) three times then we hit the break statement and that dumps us out of our for i loop as break works against the loop context we are in, not the one geographically closest to. Note that you never get to print(&#39;bye&#39;) as the break dumped us out of the loop.

Now for block 3:

for i in range(6):
    print(&#39;hello&#39;)
    if i&lt;=3:
        for j in range(3):
            print(&#39;hi&#39;)
    break                      
    print(&#39;bye&#39;)

We get the same output "hello" and 3 x "hi" but the break dump happens at a slightly different point in time. Note we still never hit print(&#39;bye&#39;).

In this 3rd block, rather than the break being the last thing we do inside our if statement, it is now the first thing we do after the if statement.

答案3

得分: 0

break语句只能用于循环,不能用于条件语句。

在示例1中,您的break语句是嵌套的for循环的一部分。

在示例2和3中,您的break语句位于嵌套的for循环之外,唯一可以连接的循环是您的第一个for循环。

一旦退出原始循环,就没有其他内容可运行,程序就结束了。

英文:

The break statement can only be used on loops. Not If statements.

In example 1 your break statement is part of your nested for loop.

In example 2 and 3 your break statement falls outside of your nested for loop and the only other loop to attach itself is your first for loop.

Once that original loop is broken out of there is nothing else to run and the program ends.

huangapple
  • 本文由 发表于 2023年6月22日 01:48:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定