Swift break语句介绍用法示例

【Swift break语句介绍用法示例】Swift 4 break语句在两种情况下使用:

  1. 当你必须立即终止语句时, 可以在循环内使用break语句。程序控制在循环后的下一条语句处恢复。
  2. 它还用于终止switch语句中的个案。
在嵌套循环的情况下, break语句终止最内层的循环并开始执行该块之后的代码的下一行。
句法:
Swift 4 break语句的语法为:
break

Swift 4 break语句流程图
Swift break语句介绍用法示例

文章图片
例:
var index = 10repeat {index = index + 1if( index == 25 ){break}print( "Value of index is \(index)")} while index < 30

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

    推荐阅读