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!")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值