牛骨文教育服务平台(让学习变的简单)

Control Flow

let base = 3
let power = 10
var answer = 1
for _ in 1...power {
    answer *= base
}
println("(base) to the power of (power) is (answer)")
// prints "3 to the power of 10 is 59049

switch中的case情况要穷尽所有的可能性,如果可以穷尽(比如case是enum类型的有限几个值)则可以不加default,否则一定要加default。case中可以使用区间,开闭都可以。

let count = 3_000_000_000_000
let countedThings = "stars in the Milky Way"
var naturalCount: String
switch count {
case 0:
    naturalCount = "no"
case 1...3:
    naturalCount = "a few"
case 4...9:
    naturalCount = "several"
case 10...99:
    naturalCount = "tens of"
case 100...999:
    naturalCount = "hundreds of"
case 1000...999_999:
    naturalCount = "thousands of"
default:
    naturalCount = "millions and millions of"
}
println("There are (naturalCount) (countedThings).")
// prints "There are millions and millions of stars in the Milky Way.

let somePoint = (1, 1)
switch somePoint {
case (0, 0):
    println("(0, 0) is at the origin")
case (_, 0):
    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

Unlike C, Swift allows multiple switch cases to consider the same value or values. In fact, the point (0, 0) could match all four of the cases in this example. However, if multiple matches are possible, the first matching case is always used. The point (0, 0) would match case (0, 0) first, and so all other matching cases would be ignored.

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))")
}
// prints "on the x-axis with an x value

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 

let integerToDescribe = 5
var description = "The number (integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
    description += " a prime number, and also"
    fallthrough
default:
    description += " an integer."
}
println(description)
// prints "The number 5 is a prime number, and also an integer.

gameLoop: while square != finalSquare {
    if ++diceRoll == 7 { diceRoll = 1 }
    switch square + diceRoll {
    case finalSquare:
        // diceRoll will move us to the final square, so the game is over
        break gameLoop
    case let newSquare where newSquare > finalSquare:
        // diceRoll will move us beyond the final square, so roll again
        continue gameLoop
    default:
        // this is a valid move, so find out its effect
        square += diceRoll
        square += board[square]
    }
}
println("Game over!")