首页 > 代码库 > 控制流 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