我是靠谱客的博主 安静小懒猪,这篇文章主要介绍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

最后

以上就是安静小懒猪最近收集整理的关于swift 控制转移 switch fallthrough while退出循环 标记的全部内容,更多相关swift内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(156)

评论列表共有 0 条评论

立即
投稿
返回
顶部