码迷,mamicode.com
首页 > 编程语言 > 详细

Swift控制语句

时间:2015-03-12 01:08:59      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:swift控制语句

1、分支语句

            

        (1)条件语句if


                if


                else


                  if else



            (2)多分支语句switch


                switch语句可以使用整数,浮点数,字符,字符串和元组等类型,而且它的数值可以是离散的也可以是连续的范围,


                case分支不需要显式的添加break语句

                

                每个case后可以添加多个值,多个值之间用逗号分隔,每个switch必须有一个default语句,它放在所有分支后面,至少要有一个case语句


            (3)在switch中使用范围匹配

                      闭区间:....

                                 90...100 表示 90>=   =<100

            

                        半闭区间:..<   90..<100  表示 90<   <100



           

                        在case中使用下划线(_)忽略其中的字段值:

                                 var student=("id":"1002","name":"李四","age":"32","ChineseScore":80,"EnglishScore":90)


                                switch student{

                                            case (_,_,_,90...100,90...100):

                                                    descr="优"

                                              case(_,_,_,80...90,80...90):

                                                      descr="良"

                                                case(_,_,_,60...80,60...80):

                                                        descr="中"

                                                  case(_,_,_,60...80,90...100),(_,_,_,90...100,60...80):

                                                        descr="偏科"

                                                    case(_,_,_,0...60,90...100),(_,_,_,90...100,0...60):

                                                          descr="严重偏科"

                                                     default:

                                                            descr="无"


}

        


                        使用逗号分隔两个元组值,表示或关系


            (4)在Switch中比较元组的值绑定


                        使用元组的时候可以在case分支中将匹配的值绑定到一个临时的变量或常量,这些常量或变量能够在分支中使用,这成为值绑定


                                         var student=("id":"1002","name":"四","age":"32","ChineseScore":80,"EnglishScore":90)

                        switch student{

                            case (_,_,let age,90...100,90...100):

                                    if (age > 30){

                        descr="老人"

}

                                else{

                            descr="小孩"


}


}


            (5)在Switch中比较元组

                

                        在绑定元组值的时候,还可以在case中使用where语句,进行条件的过滤,类似于SQL语句中的where


                    switch student{

                                case (_,_,let age,90...100,90...100)where age >0:

                                                descr="优"




}





2、循环语句


        (1)while语句


                    while 循环条件{

                        语句组


}


        (2)do while语句


                do {


}while 条件


        (3)for语句


                    for 初始化;循环条件;迭代{


    }



        (4)for in语句


                



3、跳转语句


        (1)break语句


    break可以应用于switch语句和循环语句


                    label1: for var x=0;x<5;x++{

                            label2:for var y=5;y>0;y--{

                                            if (x==y){

                                            break label1


}

                                        println("(x.y)=(\(x),\(y))")

                            

}


}


        (2)continue语句


        (3)fallthrough语句

                   贯通

                     让循环语句继续往下走


                        case 3:


                                i++

                                fallthrough


                           case 4:

                                 i++

                                fallthrough

                                    





本文出自 “平凡之路” 博客,请务必保留此出处http://linjohn.blog.51cto.com/1026193/1619485

Swift控制语句

标签:swift控制语句

原文地址:http://linjohn.blog.51cto.com/1026193/1619485

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!