Swift_for while循环,if语句


//TODO:条件语句
    func if_Swich_ForIn_do(){
        
        
        var e = ["d","aa","cc","bb"]
        
        //for...in..
        for s in e {
            
            //if...else..
            if s == "d" {
                print("s == d")
            }
            else
            {
                print(s)
            }
        }
        
        
        //while
        let str:String = ""
        var i = 0
        while (i <= e.count-1) {
            print(e[i])
            i += 1
        }
        
        
        //递增
        for index in 0 ..< 3 {
            print("index is \(index)") //0 1 2
        }
        
        for y  in 0 ... 3 {
            print("y is \(y) \n") //0 1 2 3
        }
        
        for (index, i) in (1...10).enumerated(){
            print(index)// 遍历索引  0 1 2 3 4 5 6 7 8 9
            print(i) // 遍历元素  1 2 3 4 5 6 7 8 9 10
            
        }
        
        for i in (1...10).filter({ i in i % 2 == 0}) {
            print(i)
            //输出结果为1-10之间的偶数
        }
        ///补充一个漂亮的语法糖///
        for i in (1...10).filter({ $0 % 2 == 0}) {//swift闭包可以省略参数且用$0匿名,第二个参数则用$1,以此类推
            print(i)
            //输出结果为1-10之间的偶数
        }
        
        
        for t in (1...5).filter({$0 == 2}) {
            print(t)
        }
        
        for t in 1...5 {
            if t == 2{
                print(t)
            }
        }
        
        
        //递归
        for index in (0...3).reversed()  {
            print(index) // 3 2 1 0
        }
        
        for index in (0..<3).reversed()  {
            print(index) // 2 1 0
        }

//switch
        let leng = (1,3)
        switch leng {
        case (0,1):
            break
        case (1,3):
            break
        case (_,3):  //下 _ 线 匹配所有的值
            break

        default:
            break
        }
        
        let cat = 30
        switch cat {
        case 10,20,1,2,3,4,5:
            break
        case 11,21,31,30:
            break
        default:
            break
            
        }
        
        
        
        //continue 结束当次循环,立马进入下一次循环
        for a in 1..<10 {
            
            if a == 1
            {
               continue
            }
            
            
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值