swift學習筆記4 控制流

注:英文部分來自官方文檔

switch

  • fallthrough

    In contrast with switch statements in C and Objective-C, switch statements in Swift do not fall through the bottom of each case and into the next one by default. Instead, the entire switch statement finishes its execution as soon as the first matching switch case is completed, without requiring an explicit break statement. This makes the switch statement safer and easier to use than the one in C and avoids executing more than one switch case by mistake.


與c 和oc相比,swift中 的switch並不會默認的從一個case跳轉到下一個case。而是整個switch語句只要找到匹配的case就會完成執行過程,並不需要一個顯式的break聲明。

If you need C-style fallthrough behavior, you can opt in to this behavior on a case-by-case basis with the fallthrough keyword. The example below uses fallthrough to create a textual description of a number.

如果你需要使用c風格的fallthrough 行爲,你可以在case後面加上fallthrough關鍵字。
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."
}
print(description)
// Prints "The number 5 is a prime number, and also an integer."

Although break is not required in Swift, you can use a break statement to match and ignore a particular case or to break out of a matched case before that case has completed its execution.

雖然break在swift中不是必須的,但是你依然可以使用break聲明來匹配或忽略那些特定的case,或者在匹配的case完成操作前從中退出。

The body of each case must contain at least one executable statement. It is not valid to write the following code, because the first case is empty:
每個case的內部必須包含至少一個可執行的語句。下面的代碼中,第一個case是空的,所以是無效的:

let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a": //無效,該case內部爲空
case "A":
    print("The letter A")
default:
    print("Not the letter A")
}
//這會報一個編譯時錯誤.

To make a switch with a single case that matches both “a” and “A”, combine the two values into a compound case, separating the values with commas.

爲了使switch能在一個case同時匹配a 和 A, 可以把兩個值組合到一個case裏,用逗號隔開:
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a", "A":
    print("The letter A")
default:
    print("Not the letter A")
}
// Prints "The letter A"
  • Labeled Statements

In Swift, you can nest loops and conditional statements inside other loops and conditional statements to create complex control flow structures. However, loops and conditional statements can both use the break statement to end their execution prematurely. Therefore, it is sometimes useful to be explicit about which loop or conditional statement you want a break statement to terminate. Similarly, if you have multiple nested loops, it can be useful to be explicit about which loop the continue statement should affect.

在swift中,你可以在別動循環和條件語句中嵌套循環和條件語句,以此創造複雜的控制流結構。但是,循環和條件語句都可以用break來結束操作。因此,顯式地標明哪一個循環或條件語句是你想要結束的就很有用。同樣地,如果你有多個嵌套的循環,顯式的標明continue語句作用於哪個循環也很有用

To achieve these aims, you can mark a loop statement or conditional statement with a statement label. With a conditional statement, you can use a statement label with the break statement to end the execution of the labeled statement. With a loop statement, you can use a statement label with the break or continue statement to end or continue the execution of the labeled statement.

爲了達到這一目標,你可以用一個語句標籤(statement label)來標記一個循環或者條件語句。對於條件語句,你可以使用一個帶有break語句的語句標籤來結束這個被標記的語句。對於循環語句,你可以使用帶有break或continue語句的語句標籤來結束或跳過這個被標記的語句。
下面是一個while循環的例子,對於其他的循環和switch語句,語法規則一樣適用。

label name: while condition {
    statements
}

更完整的例子

let finalSquare = 25
var board = [Int](repeating: 0, count: finalSquare + 1)
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
var square = 0
var diceRoll = 0
gameLoop: while square != finalSquare {
    diceRoll += 1
    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]
    }
}
print("Game over!")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章