Break 语句

2025 年 3 月 17 日 | 阅读 1 分钟

Swift 4 break 语句用于两种情况

  1. 当你需要立即终止语句时,可以在循环中使用 break 语句。程序控制在循环之后的下一条语句处恢复。
  2. 它也用于终止 switch 语句中的 case。

在嵌套循环的情况下,break 语句终止最内层的循环并开始执行代码块之后的下一行代码。

语法

Swift 4 break 语句的语法是

Swift 4 break 语句的流程图

Swift Break Statement

示例

输出

Value of index is 11
Value of index is 12
Value of index is 13
Value of index is 14
Value of index is 15
Value of index is 16
Value of index is 17
Value of index is 18
Value of index is 19
Value of index is 20
Value of index is 21
Value of index is 22
Value of index is 23
Value of index is 24

下一主题Continue 语句