码迷,mamicode.com
首页 > 其他好文 > 详细

控制流 control flow

时间:2015-01-06 19:33:06      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/actionke/p/4201108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!