控制流

for in 和c用法一致

特殊:

for tickMark in stride(from: 0, to: 12, by: Interval) {

   

    print(tickMark)

}

for tickMark in stride(from: 0, through: 12, by: Interval) {

   

    print(tickMark)

}

 

2

范围定义

2.1swift中的switch 不用使用break 

 

2.2如果要向下继续执行,可以使用fallthrough 需注意的是 fallthrough 只能向下执行一次case 且不会做条件判断

 

2.3如果在控制流内使用控制流(嵌套) 可以为控制流添加标签 例如 gameLoop:while

 

例子: 元组类型

let person = ("Tony",15)

 

switch person {

 

case ("Tony",15):

   

    print("我是Tony")

   

case (_,15):

   

    print("和tony 一般大")

case ("Tony",_):

   print("和tony 同名")

   

default:

   

    print("null")

   

}

 

值绑定

let origin = (2,3)

 

switch origin {

 

case (let x , 5):

   

    print("x==\(x)")

   

case (1 , let y):

   

    print("y==\(y)")

   

case (let x,let y):

   

   print("x==\(x)==\(y)")

}

 

结合where使用

 

let yetAnotherPoint = (1, -1)

 

switch yetAnotherPoint {

case let (x, y) where x == 1:

    print("(\(x), \(y)) is on the line x == y")

case let (x, y) where x == -y:

    print("(\(x), \(y)) is on the line x == -y")

case let (x, y):

    print("(\(x), \(y)) is just some arbitrary point")

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值