美文网首页
04-Swift条件控制

04-Swift条件控制

作者: 一抹相思泪成雨 | 来源:发表于2020-12-12 23:17 被阅读0次

    1.if-else

    使用方式:

    • if后面的条件可以省略小括号
    • 条件后面的大括号不可以省略
    • if后面的条件只能是Bool类型
    let age = 4
    if age >= 22 {
        print(">= 22")
    }else if age >= 18 {
        print(">= 18")
    }else if age >= 7{
        print(">= 7")
    }else{
        print("other")
    }
    
    

    2.while

    使用方式:

    • repeat-while相当于C语言中的do-while
    • 从Swift3开始,去除了自增(++)、自减(--)运算符
      1.不同编译器结果不同
      2.自增(++)、自减(--) 可读性差、使用复杂
    var num = 5
    while num > 0 {
        print("num is \(num)")
        num -= 1 
    } // 打印了5次
    
    var num = -1 
    repeat {
        print("num is \(num)")
     } while num > 0 // 打印了1次
    

    3.for

    使用方式:

    • 闭区间运算符:a...b, a <= 取值 <= b
    • 半开区间运算符:a..<b, a <= 取值 < b
    let names = ["小明","小红","小张","小白"]
    /// for in  半开区间
    for i in 0 ..< 3 {
       print(names[i])
    }
    
    for items in names {
       print(items)
    }
    /// for in enumerated
    for (index, value) in names.enumerated() {
       print("index =", index, "value =", value)
    }
    
    /// forEach
    names.forEach { (item) in
       print(item)
    }
    
    // $0 变量
    names.forEach { 
       print($0)
    }
    
    /// stride to (不包含)
    for index in stride(from: 1, to: 4, by: 1) {
       print(index)
    }
    
    /// stride through (包含)
    for index in stride(from: 0, through: 4, by: 2) {
       print(index)
    }
    
    3.1 for – 区间运算符用在数组上
    for name in names[2...] { // 单侧区间,让区间朝一个方向尽可能的远
       print(name)
    }
    for name in names[...2] {
       print(name)
    }
    for name in names[..<2] {
       print(name)
    }
    
    3.2 i默认是let,需要也可以声明为var
    //: i默认是let,需要也可以声明为var
    for var i in 1...3 { // 闭区间包含 1和3
       i += 5 // 操作i 需变为var类型
       print(i)
    }/// 6 7 8
    
    

    4.range的使用

    • 字符、字符串也能使用区间运算符,默认不能用在for-in中
    let range = 1...3
    for i in range {
       print(names[i])
    } 
    
    let range = ...5 
    range.contains(7) // false 
    range.contains(4) // true
    range.contains(-3) // true
    
    let stringRange1 = "cc"..."ff" // 
    ClosedRange<String> stringRange1.contains("cb") // false 
    stringRange1.contains("dz") // true 
    stringRange1.contains("fg") // false
    
    let stringRange2 = "a"..."f" 
    stringRange2.contains("d") // true 
    stringRange2.contains("h") // false
    

    5.switch

    • case、default 后面 不能写大括号{} 默认可以不写break,并不会贯穿到后面的条件
    • switch必须要 保证能处理所有情况
    • case、defaut后面 至少要有一条语句
    • 如果不做任何事, 加个break即可
    • 如果能保证已处理所有情况,也可以 不使用default
    var number = 1
    switch number {
    case 1:
       print("number is 1")
    case 2:
       print("numebr is 2")
    default:
       print("number is other")
    }
    
    //: fallthrough 可以实现贯穿效果
    switch number {
    case 1:
       print("number is 1")
       fallthrough
    case 2:
       print("number is 2")
    default:
       print("number is other")
    }
    
    

    6.复合条件

    • switch也支持Character、String类型
    let string = "Jack"
    switch string {
    case "Jack", "Rose":
       print("Right person")
    default:
       break
    } // Right person
    
    let character: Character = "a"
    switch character {
    case "a", "A":
       print("The letter A")
    default:
       print("Not the letter A")
    } // The letter A
    

    7.区间匹配、元组匹配

    • 可以使用下划线 _ ,忽略某个值
    let count = 62
    switch count {
    case 0:
       print("none")
    case 1..<100: // 区间匹配
       print("a few")
    case 100..<1000:
       print("handreds of")
    default:
       print("many")
    }
    
    var point = (1, 1)
    switch point {
    case (0, 0): // 元组匹配
       print("the origin")
    case (_, 0):
       print("on the x-axis")
    case (0, _):
       print("on the y-axis")
    case (-2...2, -2...2):
       print("inside the box")
    default:
       print("outside of the box")
    }// inside the box
    

    8.值绑定

    • 必要时let、也可以改为var
    point = (2, 0) // 一个符合就行
    switch point {
    case (let x, 0):
       print("on the x-axis with an x value of \(x)") // 将x值拿出直接使用
    case (0, let y):
       print("on the y-axis with y value of \(y)")
    case let(x, y):
       print("somewhere else at(\(x), \(y))")
    }
    

    9.where

    point = (1, -1)
    switch point {
    case let(x, y) where x == y:
       print("on the line x == y")
    case let(x, y) where x == -y:
       print("on the line x == -y")
    case let (x, y):
       print("\(x), \(y) is just some arbitrary point")
    }
    
    let numbers = [-1,2,5]
    var sum = 0;
    for num in numbers where num > 0 { // where 来过滤num 相当于 continue
       sum+=num;
    }
    print(sum)
    

    10.标签语句outer

    outer: for i in 1...4 { // 外循环添加标签
       for k in 1...4 {
           if k == 3 {
               continue outer  // 结束本次循环,跳到外循环
           }
           if i == 3 {
               break outer  // 结束外循环
           }
           print("i == \(i), k == \(k)")
       }
    }
    

    相关文章

      网友评论

          本文标题:04-Swift条件控制

          本文链接:https://www.haomeiwen.com/subject/zgiowktx.html