Swift 4 break语句在两种情况下使用:
- 当你必须立即终止语句时, 可以在循环内使用break语句。程序控制在循环后的下一条语句处恢复。
- 它还用于终止switch语句中的个案。
在嵌套循环的情况下, 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