[英]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
块。
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
加到列表中并再次运行,它将break
, else
将不打印。 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
,然后如果found
为False
则仅在循环之后打印语句。
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部分:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.