c语言中break可以用在swift,简单了解Swift语言中的break和continue语句的用法

break语句在 C 编程语言中的 break 语句有以下两种用法:

当在循环中遇到 break 语句, 循环立即终止,程序控制继续循环语句的后面(退出循环)。

它可用于终止在switch语句(在下一章节)的情况(case)。

如果使用嵌套循环(即,一个循环在另一个循环), break语句将停止最内层循环的执行,并开始执行下一行代码块之后的代码块。

语法

在Swift 编程中的 break语句的语法如下:

break

流程图

05a80a4df2a0cbf236ed804de827c609.png

实例

import Cocoa

var index = 10

do{

index = index + 1

if( index == 15 ){

break

}

println( "Value of index is \(index)")

}while index < 20

当上述代码被编译和执行时,它产生了以下结果:

Value of index is 11

Value of index is 12

Value of index is 13

Value of index is 14

continue语句在 Swift 编程语言中的 continue 语句告诉循环停止正在执行的语句,并在循环下一次迭代重新开始。

对于 for 循环,continue 语句使得循环的条件测试和增量部分来执行。对于 while 和 do ... while 循环,continue 语句使程序控制转到条件测试。

语法

在 Swift 中的 continue 语句的语法如下:

continue

流程图

44dd28a8182d5e1f20e0a13bf124f0b1.png

实例

import Cocoa

var index = 10

do{

index = index + 1

if( index == 15 ){

continue

}

println( "Value of index is \(index)")

}while index < 20

当上述代码被编译和执行时,它产生了以下结果:

Value of index is 11

Value of index is 12

Value of index is 13

Value of index is 14

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值