• Swift


    一、if 语句

     let count = 7
            
            if count > 1{
                print("yes")
            }else{
                print("no")
            }
    

    二、switch 语句 

    (1)Swift中不需要在case块中显示地使用break跳出switch。 

    let fruit = "apple"
            switch fruit {
            case "aaple":
                print("good")
            case  "banana","orange":
                print("great")
            default:
                print("bad")
                
            }
    

     (2)case分支还可以进行区间匹配

    let age = 3
            switch age {
            case 0...11:
                print("正太")
            case 12...13:
                print("少年")
            default:
                print("中年")
            }
    

     (3)case分支同样支持单侧区间匹配

    let num = 2
            switch num {
            case ..<0:
                print("负数")
            case 0:
                print("零")
            case 0...:
                print("正数")
            default:
                print("未知")
            }
    

     (4)使用元组匹配(判断属于哪个象限)

    let point = (2,2)
            switch point {
            case (0,0):
               print("坐标在原点")
            case (_,0):
               print("坐标在x轴上")
            case (0,_):
               print("坐标在y轴上")
            case (-2...2,-2...2):
                print("坐标在长宽为4的正方形内")
            default:
                print("未知")
            }
    

     (4)case中还可以使用where关键字来做额外的判断条件

      var height = 2.0
            switch height {
            case 1...3 where height == 1.72:
                print("case 1")
            case 1...3 where height == 2.0:
                print("case 2")
            default:
                print("default")
            }
    

    三、for 循环语句 

    (1)for-in循环

     for i in 1..<10{
                print("(i)")
            }
           //遍历数组
            let numbers = [1,2,4,7]
            for num in numbers{
                print("(num)")
            }
    
            //遍历字典
            let nameOfAge = ["lily":18,"Candy":24]
            for(aName, iAge)in nameOfAge{
                print("(aName) is (iAge)")
            }
    
            //遍历字符串的字符
            "helloWorld".forEach { (C) in
                 print(C)
            }
            (1...10).forEach {
                print($0)
            }
    

    四、while 循环语句

    var i = 0
            
            while i < 10 {
                i += 1
                print(i)
            }
            
            repeat{
              i += 1
            print(i)
            }while i<100
    
  • 相关阅读:
    关于Oracle
    form表单中包含特殊字符,需要转义。
    mysql5.7解压版安装步骤
    mysql报1055错误
    配置maven私有仓库
    全选,反选
    前后端数据交互(json)
    正则表达式匹配html标签里的中文
    excel创建行、插入行、设置样式
    Python 中文字符的输出
  • 原文地址:https://www.cnblogs.com/baidaye/p/8616579.html
Copyright © 2020-2023  润新知