programming-languages - 哪些语言具有 while-else 类型的控制结构,它是如何工作的?

标签 programming-languages for-loop language-features if-statement

很久以前,我以为我看到了一个提议,在 C 或 C++ 中的 elsefor 循环中添加一个 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/

相关文章:

java - 系统端编程 - 哪种语言?

c - c中的隐式转换和显式转换

javascript - 循环应该创建 10 个段落并插入文本,但文本只出现在一个中

Java继承与OOP和AOP

language-features - 支持 bool 语法糖的语言

oop - 接口(interface)是否与多重继承冗余?

java - 员工入围

programming-languages - 基于事件 == 异步?

Python for 循环跳过所有其他值

java - 如果在 for 语句中使用了 StatementExpressionList,ForInit 的值何时会被丢弃?