zoukankan      html  css  js  c++  java
  • ios开发swift学习第三天:逻辑分支

    一. 分支的介绍

    • 分支即if/switch/三目运算符等判断语句
    • 通过分支语句可以控制程序的执行流程

    二. if分支语句

    • 和OC中if语句有一定的区别
      • 判断句可以不加()
      • 在Swift的判断句中必须有明确的真假
        • 不再有非0即真
        • 必须有明确的Bool值(swift中的Bool值用false/true来表示)
        • Bool有两个取值:false/true
      • // 演练一:
        let a = 10
        
        // 错误写法:
        //if a {
        //    print("a")
        //}
        
        // 正确写法
        if a > 9 {
            print(a)
        }
        
        // 演练二:
        let score = 87
        
        if score < 60 {
            print("不及格")
        } else if score <= 70 {
            print("及格")
        } else if score <= 80 {
            print("良好")
        } else if score <= 90 {
            print("优秀")
        } else {
            print("完美")
        }
        
        // 演练三:
        // 这个是可选类型,因为只有声明成可选类型后,才可以判断是否为空
        // 可选类型会在后续讲解,可先了解即可
        let view : UIView? = UIView()
        
        // 判断如果view有值,则设置背景
        // 错误写法
        //if view {
        //    view.backgroundColor = UIColor.redColor()
        //}
        
        if view != nil {
            view!.backgroundColor = UIColor.redColor()
        }
    import UIKit
    
    // 1.if的使用
    // 1> if后面的()可以省略
    // 2> 判断句不再有非0/nil即真.必须有明确的Bool值:true/false
    let a = 10
    
    if a != 0 {
        print("a不等于0")
    } else {
        print("a等于0")
    }
    
    // 2.if elseif的使用
    let score = 88
    
    if score < 0 || score > 100 {
        print("没有意义的分数")
    } else if score < 60 {
        print("不及格")
    } else if score < 80 {
        print("及格")
    } else if score < 90 {
        print("良好")
    } else if score <= 100 {
        print("优秀")
    }
    
    // 3.三目运算符
    let m = 40
    let n = 30
    
    var result = 0
    
    //if m > n {
    //    result = m
    //} else {
    //    result = n
    //}
    
    result = m > n ? m : n
    
    print(result)

    三:guard的使用

    • guard是Swift2.0新增的语法
    • 它与if语句非常类似,它设计的目的是提高程序的可读性
    • guard语句必须带有else语句,它的语法如下:
      • 当条件表达式为true时候跳过else语句中的内容,执行语句组内容
      • 条件表达式为false时候执行else语句中的内容,跳转语句一般是return、break、continue和throw
    guard 条件表达式 else {
        // 条换语句
        break
    }
    语句组

    //例子:
    var age = 18
    func online(age : Int) -> Void {
        guard age >= 18 else {
            print("回家去")
            return
        }
    
        print("可以上网")
    }
    
    online(age)

    import UIKit
    
    // 1.guard必须用在函数
    let age = 20
    
    //func online(age : Int) {
    //    if age >= 18 {
    //        if 带了身份证 {
    //            if 带了钱 {
    //                print("可以上网")
    //            } else {
    //                print("不可以上网,回家拿钱")
    //            }
    //        } else {
    //            print("不可以上网,回家拿身份证")
    //        }
    //    } else {
    //        print("不可用上网,回家去吧")
    //    }
    //}
    
    func online(age : Int) {
        //guard必须用在函数中,必须和else连用,就相当于简化了多个if else的判断
        // 如果条件成立,者会执行后面的代码块
        // 如果条件不成立,则会执行else{}中的语句,并且在else{}中必须跟上return,continue,break,throw跳转语句
        guard age >= 18 else {
            print("未成年不能上网")
            return
        }
        
        guard 带了身份证 else {
            print("不可以上网,回家拿身份证")
            return
        }
        
        guard 带了钱 else {
            print("回家拿钱")
            return
        }
        
        print("留下来上网")
    }

    四.switch分支

    switch的介绍
    • Switch作为选择结构中必不可少的语句也被加入到了Swift中
    • 只要有过编程经验的人对Switch语句都不会感到陌生
    • 但苹果对Switch进行了大大的增强,使其拥有其他语言中没有的特性
    switch的简单使用
    • 基本用法和OC用法一致
    • 不同之处:
      • switch后可以不跟()
      • case后可以不跟break(默认会有break)
    • 例子:
    let sex = 0
    
    switch sex {
    case 0 :
        print("男")
    case 1 :
        print("女")
    default :
        print("其他")
    }
    

    • 简单使用补充:
      • 一个case判断中,可以判断多个值
      • 多个值以,隔开
    let sex = 0
    
    switch sex {
    case 0, 1:
        print("正常人")
    default:
        print("其他")
    }
    
    • 简单使用补充:
      • 如果希望出现之前的case穿透,则可以使用关键字fallthrough,fallthrough会执行完case0以后,再继续执行case1
    let sex = 0
    
    switch sex {
    case 0:
        fallthrough
    case 1:
        print("正常人")
    default:
        print("其他")
    }
    
    Switch支持多种数据类型
    • 浮点型的switch判断
    let f = 3.14
    switch f {
    case 3.14:
        print("π")
    default:
        print("not π")
    }
    

    • 支持字符串类型
      • 字符串的使用后面会详细讲解
    let m = 5
    let n = 10
    var result = 0
    
    let opration = "+"
    
    switch opration {
        case "+":
            result = m + n
        case "-":
            result = m - n
        case "*":
            result = m * n
        case "/":
            result = m / n
    default:
        result = 0
    }
    
    print(result)
    
    switch支持区间判断
    • 什么是区间?
      • 通常我们指的是数字区间:0~10,100~200
    • swift中的区间常见有两种
      • 开区间:0..<10 表示:0~9,不包括10
      • 闭区间:0...10 表示:0~10
    let score = 88
    
    switch score {
    case 0..<60:
        print("不及格")
    case 60..<80:
        print("几个")
    case 80..<90:
        print("良好")
    case 90..<100:
        print("优秀")
    default:
        print("满分")
    }
    

    import UIKit
    
    // 1.基本用法
    let sex = 0
    
    // 0:男 1:女 其他:其他
    
    // 1> switch可以不跟() 2> case语句结束后可以不跟break,默认系统会加
    
    switch sex {
    case 0:
        print("")
        // fallthrough
    case 1:
        print("")
    default:
        print("其他")
    }
    
    // 2.基本用法的补充:
    // 1>如果希望一个case中出现case穿透,那么可以在case语句结束后跟上fallthrough
    // 2>case后面可以跟多个条件,多个条件以,分割
    switch sex {
        case 0, 1:
            print("正常人")
        default:
            print("其他")
    }
    
    // 3.switch可以判断浮点型
    let a : Double = 3.14
    
    //if a == 3.14 {
    //    print("π")
    //} else {
    //    print("非π")
    //}
    
    switch a {
    case 3.14:
        print("π")
    default:
        print("非π")
    }
    
    // 4.switch可以判断字符串
    let m = 20
    let n = 30
    let opration = "+"
    
    var result = 0
    
    switch opration {
    case "+":
        result = m + n
    case "-":
        result = m - n
    case "*":
        result = m * n
    case "/":
        result = m / n
    default:
        print("非法操作符")
    }
    
    // 5.switch可以判断区间
    let score = 93
    
    switch score {
    case 0..<60:
        print("不及格")
    case 60..<80:
        print("及格")
    case 80..<90:
        print("良好")
    case 90...100:
        print("不错噢")
    default:
        print("不合理的分数")
    }
     
  • 相关阅读:
    显示磁盘信息
    给VG增加磁盘,给文件目录增加空间
    第8.11节 Python类中记录实例变量属性的特殊变量__dict__
    Python中format_map与format字符串格式化的区别
    生成器函数与函数的区别
    什么是Python生成器?与迭代器的关系是什么?
    第8.10节 使用__class__查看Python中实例对应的类
    Python运算符的优先级是怎样的?
    Python怎么控制将一个整数输出成指定长的十六进制数?
    Python中函数的参数带星号是什么意思?
  • 原文地址:https://www.cnblogs.com/cqb-learner/p/5881433.html
Copyright © 2011-2022 走看看