Swift break语句
Swift 4中的break语句用于以下两种情况:
- 当需要立即终止循环语句时使用break语句。程序控制在循环后的下一条语句继续执行。
- 也用于终止switch语句中的某个case。
在嵌套循环中,break语句终止最内层的循环,并开始执行代码块后的下一行。
语法
Swift 4中break语句的语法如下:
break
Swift 4 break语句的流程图
示例
var index = 10
repeat {
index = index + 1
if( index == 25 ){
break
}
print( "Value of index is \(index)")
} while index < 30
输出:
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