Swift控制语句

发布时间:2020-05-31 12:51:11 作者:hehemmm
来源:网络 阅读:238

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

                                    





推荐阅读:
  1. Swift2.0(1)Swift简介
  2. Swift嵌套函数

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

语句 控制 swift

上一篇:搭建Zabbix Proxy HA

下一篇:C++求字符串最长连续字符的长度的代码

相关阅读

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

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