很久以前,我以为我看到了一个提议,在 C 或 C++ 中的 else
或 for
循环中添加一个 while
子句......或类似的东西。我不记得它应该如何工作——如果循环正常退出但不是通过 break 语句退出,else
子句是否运行?
无论如何,这很难搜索,所以我想也许我可以在这里获得各种语言的一些 CW 答案。
哪些语言支持向 else
语句以外的内容添加 if
子句?那个条款是什么意思?请为每个答案提供一种语言。
最佳答案
Python 。
使用示例:
for element in container:
if element == target:
break
else:
# this will not be executed if the loop is quit with break.
raise ElementNotFoundError()
从 Python docs :
it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.
关于programming-languages - 哪些语言具有 while-else 类型的控制结构,它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2561578/