• Swift学习笔记(6):控制流


    目录:

    • For-In
    • While
    • If-Else, Guard-Else
    • Switch
    • 控制转移

    For-In

    可以使用for-in语句循环遍历集合、区间、元组、字符串。

    // 遍历区间,返回元素值
    for index in 1...5 {
        print("(index) times 5 is (index * 5)")
    }
    
    // 循环区间,忽略元素值
    for _ in 1...power {
        answer *= base
    }
    
    // 遍历数组
    let names = ["Anna", "Alex", "Brian", "Jack"]
    for name in names {
        print("Hello, (name)!")
    }
    
    // 遍历字典
    let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
    for (animalName, legCount) in numberOfLegs {
        print("(animalName)s have (legCount) legs")
    }
    While

    可以使用while和repeat-while语句进行条件循环遍历。

    while square < finalSquare {
        // statements ...
    }
    
    // 类似其他语言的do-while语句
    repeat {
        // statements ...
    } while square < finalSquare
    If-Else, Guard-Else
    temperatureInFahrenheit = 90
    if temperatureInFahrenheit <= 32 {
        print("It's very cold. Consider wearing a scarf.")
    } else if temperatureInFahrenheit >= 86 {
        print("It's really warm. Don't forget to wear sunscreen.")
    } else {
         print("It's not that cold. Wear a t-shirt.")
    }

    可以使用guard-else语句来要求条件必须为真时,以执行guard语句后的代码。

    // person字典中存在key为name的元素时打印name元素值
    guard let name = person["name"] else {
        return
    }
    print("Hello (name)")
    Switch

    switch语句尝试将某个值与若干个case进行匹配,然后执行第一个匹配成功的case下的代码。

    switch语句的每一个可能的值都必须至少有一个case分支与之对应。如果不可能涵盖所有值的情况时,在switch语句最后面使用默认default分支。

    switch语句不存在隐式贯穿,即不需要在case下的执行代码中显示使用break。

    let someCharacter: Character = "z"
    switch someCharacter {
    case "a":
        print("The first letter of the alphabet")
    // case "b": 打开注释后会有编译错误,不存在隐式贯穿
    case "c","z":    // 复合匹配
        print("The last letter of the alphabet")
    default:
        print("Some other character")
    }

    case分支的模式也可以是一个值的区间或元组:

    // case分支匹配区间
    switch approximateCount {
    case 0:
        naturalCount = "no"
    case 1..<5:
        naturalCount = "a few"
    default:
        naturalCount = "many"
    }
    
    // case分支匹配元组
    let somePoint = (1, 1)
    switch somePoint {
    case (0, 0):  // 元组元素是值类型
        print("(0, 0) is at the origin")
    case (_, 0):// 元组元素使用 _ 匹配所有任意值
        print("((somePoint.0), 0) is on the x-axis")
    case (-2...2, -2...2): // 元组元素是区间类型
        print("((somePoint.0), (somePoint.1)) is inside the box")
    }

    case分支允许将匹配的值绑定到一个临时的常量或变量,并且在case分支体内使用,还可以使用where来补充case匹配条件。

    let yetAnotherPoint = (1, 1)
    switch yetAnotherPoint {
    case (let x, 0):
        print("on the x-axis with an x value of (x)")
    case let (x, y) where x == y: // 输出 "(1, -1) is on the line x == y"
        print("((x), (y)) is on the line x == y")
    case let (x, y):
        print("((x), (y)) is just some arbitrary point")
    }
    控制转移
    • continue 结束本次循环执行下次循环
    • break 结束当前switch,while语句体,不能结束多层嵌套的switch/while/if语句体。
    • fallthrough 用在switch语句中,标示贯穿当前case到下一case中。
    • return 返回,可终止多层嵌套的switch/while/if语句体。
    • throw 抛出异常,结束执行后面的代码。

     continue和break都可以跳转到指定标签的位置执行。

    声明:该系列内容均来自网络或电子书籍,只做学习总结!
  • 相关阅读:
    Windows NT 的历史
    Windows 下 Mysql8.0.12 的安装方法
    [知乎] 机械硬盘和固态硬盘的简单对比
    syncthing 多主机同步文件工具
    delphi 2007 远程调试
    关于delphi PAServer 远程调试DLL文件
    关于 datasnap Stream的英文博客能容
    DataSnap 用TStream 传递大数据 返回流大小为-1的情况
    c++ builder xe2 (Embarcadero rad studio) 远程调试 同样适用于 delphi 远程调试 教程
    Delphi10 安装Graphics32
  • 原文地址:https://www.cnblogs.com/Youhei/p/6877652.html
Copyright © 2020-2023  润新知