简体   繁体   English

继续声明中的其他部分如何工作?

[英]How else part work in continue statement?

I'm not sure how the continue statement is interpreted when it is inside a for loop with an else clause. 我不确定continue语句在带有else子句的for循环中时如何解释。

If the condition is true, the break will exit from a for loop and else part will not be executed. 如果条件为真,则break将从for循环退出, else将不执行part。 And if the condition is False then else part will be executed. 如果条件为False,则将执行else部分。

But, what about continue statement? 但是, continue声明呢? I tested it seems that the after the continue statement is reached, the else part will be executed. 我测试了似乎在到达continue语句之后,将执行else部分。 Is this true?? 这是真的?? Here is a code example: 这是一个代码示例:

# when condition found and it's `true` then `else` part is executing :

edibles = ["ham", "spam", "eggs","nuts"]
for food in edibles:
    if food == "spam":
        print("No more spam please!")
        continue
    print("Great, delicious " + food)

else:
    print("I am so glad: No spam!")
print("Finally, I finished stuffing myself")`

If I remove "spam" from the list, now the condition is always false and never found but still the else part is executed: 如果我从列表中删除“垃圾邮件”,则现在条件始终为false且从未找到,但仍然执行else部分:

edibles = ["ham","eggs","nuts"]
for food in edibles:
    if food == "spam":
        print("No more spam please!")
        continue
    print("Great, delicious " + food)

else:
    print("I am so glad: No spam!")
print("Finally, I finished stuffing myself")

With a for loop in Python, the else block is executed when the loop finishes normally, ie there is no break statement. 在Python中使用for循环时, else块在循环正常结束时执行,即没有break语句。 A continue does not affect it either way. continue不会影响任何一种方式。

If the for loop ends because of a break statement, then else block will not execute. 如果for循环由于break语句而结束,则else块将不会执行。 If the loop exits normally (no break ), then the else block will be executed. 如果循环正常退出(不break ),则将执行else块。

From the docs : 文档

When used with a loop, the else clause has more in common with the else clause of a try statement than it does that of if statements: a try statement's else clause runs when no exception occurs, and a loop's else clause runs when no break occurs. 与循环一起使用时,else子句与try语句的else子句比if语句具有更多的共同点:try语句的else子句在没有异常发生时运行,而循环的else子句在没有中断时发生运行。

I always remember it because of how Raymond Hettinger describes it . 我一直记得它,因为雷蒙德•海廷格 Raymond Hettinger)是如何描述它的 He said it should have been called nobreak instead of else . 他说应该把它叫做nobreak而不是else (That's also a good video that explains the usefulness of the for-else construct) (这也是一个很好的视频,解释了for-else构造的用处)

Example: 例:

numbers = [1,2,3]
for number in numbers:
    if number == 4:
        print("4 was found")
        break
else:
    print("4 was not found")

When you run the above code, since 4 is not in the list, the loop will not break and the else clause will print. 当您运行上述代码时,由于列表中没有4 ,因此循环不会break并且会打印else子句。 If you add 4 to the list and run it again, it will break and the else will not print. 如果将4加到列表中并再次运行,它将breakelse将不打印。 In most other languages, you would have to add some sentinel boolean like found and make it True if you find a 4 , then only print the statement after the loop if found is False . 在大多数其他语言中,您将必须添加found前哨布尔值,如果找到4则将其设置为True ,然后如果foundFalse则仅在循环之后打印语句。

Your else part will be executed in both cases. 在这两种情况下,您的else部分都将执行。 else part executed when loop terminate when condition didn't found.Which is what is happening in your code. else部分在没有找到条件的情况下在循环终止时执行。这就是代码中发生的事情。 But it will also work same without continue statement. 但是,如果没有continue声明,它将同样起作用。

now what about break statement's else part, Break statement's else part will be executed only if: 现在,关于break语句的else部分,只有在以下情况下,才执行Break语句的else部分:

  • If the loop completes normally without any break. 如果循环正常完成而没有任何中断。
  • If the loop doesn't encounter a break. 如果循环没有中断。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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