Swift 控制语句

发布时间:2020-07-06 09:08:18 作者:510202
来源:网络 阅读:356

条件控制: if 和 Switch

循环控制:for-inforwhile, and do-while

1  参考代码如下:       

        let classScore = [100,90,80,70,60,50,40,30]

        var totalScore = 0

        for score in classScore{

            if score >= 60 {

                totalScore += score

            } else {

                println("no pass,score is \(score)" );

            }

        }

        println("totalScore is \(totalScore)");


 2  switch case 循环,不再限定类型:

            let vegetable = "Switch case Type"

            var result = ""

            switch vegetable{

                case "calery":

                     result = "calery"

                case "calendar","china":

                     result = "china"

                case let x where x.hasSuffix("Type"):

                     result = "haSuffix";

                default :

                     result = "default"

            }

            println("result :\(result)")


3  使用for-in 遍历字典:

        let interestingNumbers = [

            "Prime": [2, 3, 5, 7, 11, 13],

            "Fibonacci": [1, 1, 2, 3, 5, 8],

            "Square": [1, 4, 9, 16, 25],

        ]

        var largest = 0

        for (kind, numbers) in interestingNumbers {

            for number in numbers {

                if number > largest {

                    largest = number

                }

            }

        }

        println("largest:\(largest)");


4     while 和 do while代码如下:

5 for循环如下:  

                var firstLooper = 0;

                for i in 0..3{

                    println("firstLooper:\(i)")

                    firstLooper += 1;

                }

                var secondLopper = 0;

                for var i = 0;i < 3 ; ++i{

                    println("secondLopper:\(i)")

                    secondLopper += 1;

                }

                           

下一篇文章将讲解方法、函数

推荐阅读:
  1. 09.swift循环(for)
  2. Swift奠基-常量和变量

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

swift 语句控制 控制语

上一篇:Jupyter环境的默认目录和默认浏览器修改

下一篇:获取当前时间

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》