首页 > 代码库 > 控制流 control flow
控制流 control flow
1 Switch控制语句
1.1 Switch语句中Turple的使用
let somePoint = (1, 1)switch somePoint {case (0, 0): println("(0, 0) is at the origin")case (_, 0)://使用下划线(underscore)代表任何可能的值 println("(\(somePoint.0), 0) is on the x-axis")case (0, _): println("(0, \(somePoint.1)) is on the y-axis")case (-2...2, -2...2): println("(\(somePoint.0), \(somePoint.1)) is inside the box")default: println("(\(somePoint.0), \(somePoint.1)) is outside of the box")//prints "(1, 1) is inside the box"
1.2. Switch中值绑定 Value Bindings
let anotherPoint = (2, 0)switch anotherPoint {case (let x, 0): println("on the x-axis with an x value of \(x)")case (0, let y): println("on the y-axis with a y value of \(y)")case let (x, y): println("somewhere else at (\(x), \(y))")}
1.3 Switch中Where的使用
let yetAnotherPoint = (1, -1)switch yetAnotherPoint {case let (x, y) where x == y: println("(\(x), \(y)) is on the line x == y")case let (x, y) where x == -y: println("(\(x), \(y)) is on the line x == -y")case let (x, y): println("(\(x), \(y)) is just some arbitrary point")}// prints "(1, -1) is on the line x == -y"
1.4 给循环(Loop)和Switch添加标签(Lable)
当多个Loop或Switch嵌套时,可以给某个Loop或Switch绑定一个标签,这样可以使用break和continue对特定的Loop和Switch进行操作,
写法:
控制流 control flow
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。