成都网站建设设计

将想法与焦点和您一起共享

Swift控制语句

条件控制: if 和 Switch

在栾城等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、成都网站建设 网站设计制作按需求定制开发,公司网站建设,企业网站建设,成都品牌网站建设,全网整合营销推广,成都外贸网站制作,栾城网站建设费用合理。

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

1  参考代码如下:       

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

        var totalScore = 0

        for scoreinclassScore{

            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 = ""

            switchvegetable{

               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代码如下:

  • var n = 2

  • while n < 100 {

  •    n = n * 2

  • }

  • var m = 2

  • do {

  •    m = m * 2

  • } while m < 100

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;

                }

                           

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


新闻名称:Swift控制语句
网页URL:http://chengdu.cdxwcx.cn/article/gjioed.html