swift 控制转移 switch fallthrough while退出循环 标记

swift 中的switch语句 去掉了break语法

但是如果遇到了需要case穿透的地方

可以添加此关键字 fallthrough

code

//: A UIKit based Playground for presenting user interface

import UIKit

let somePoint=(2,-2)
switch somePoint {

    case let(x,y) where x==y :
        print("(\(x),\(y))is on the line x == y")
    fallthrough
    
    case let(x,y) where x == -y:
     print("(\(x),\(y))is on the line x == -y")
    fallthrough
case let(x,y):
    print("(\(x),\(y))is just some arbitrary point")

}

(2,-2)is on the line x == -y

(2,-2)is just some arbitrary point

退出循环可以通过break标记来达到目地

//: A UIKit based Playground for presenting user interface

import UIKit

var number=10
whileLoop:while number>0{
    switch number {
    case 9:
        print("9")
    case 10:
        var sum=0
        for index in 0...10{
            sum+=index
    
            if index==9{
                print(sum)
                break whileLoop
            }
        }
    default:
        break
    }
    number -= 1
}

结果:45