目录:

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

For-In

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

// 遍历区间,返回元素值
for index in ... {
print("(index) times 5 is (index * 5)")
} // 循环区间,忽略元素值
for _ in ...power {
answer *= base
} // 遍历数组
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
print("Hello, (name)!")
} // 遍历字典
let numberOfLegs = ["spider": , "ant": , "cat": ]
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 =
if temperatureInFahrenheit <= {
print("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= {
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 :
naturalCount = "no"
case ..<:
naturalCount = "a few"
default:
naturalCount = "many"
} // case分支匹配元组
let somePoint = (, )
switch somePoint {
case (, ): // 元组元素是值类型
print("(0, 0) is at the origin")
case (_, ):// 元组元素使用 _ 匹配所有任意值
print("(\(somePoint.0), 0) is on the x-axis")
case (-..., -...): // 元组元素是区间类型
print("(\(somePoint.0), \(somePoint.1)) is inside the box")
}

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

let yetAnotherPoint = (, )
switch yetAnotherPoint {
case (let x, ):
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都可以跳转到指定标签的位置执行。

声明:该系列内容均来自网络或电子书籍,只做学习总结!

Swift学习笔记(6):控制流的更多相关文章

  1. swift学习笔记之控制流

    控制流: 1.if语句 let count = { print("yes") }else{ print("no") } 2.switch语句 (1)Swift中 ...

  2. Swift学习笔记(7)--控制流

    1.For循环 //1.条件递增 for var index = 0; index < 3; ++index { println("index is \(index)") } ...

  3. 【swift学习笔记】二.页面转跳数据回传

    上一篇我们介绍了页面转跳:[swift学习笔记]一.页面转跳的条件判断和传值 这一篇说一下如何把数据回传回父页面,如下图所示,这个例子很简单,只是把传过去的数据加上了"回传"两个字 ...

  4. Swift学习笔记(一)搭配环境以及代码运行成功

    原文:Swift学习笔记(一)搭配环境以及代码运行成功 1.Swift是啥? 百度去!度娘告诉你它是苹果最新推出的编程语言,比c,c++,objc要高效简单.能够开发ios,mac相关的app哦!是苹 ...

  5. swift学习笔记1——基础部分

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  6. Swift学习笔记一

    最近计划把Swift语言系统学习一下,然后将MagViewer用这种新语言重构一次,并且优化一下,这里记录一下Swift的学习笔记. Swift和Objective-C相比,在语法和书写形式上做了很多 ...

  7. swift学习笔记5——其它部分(自动引用计数、错误处理、泛型...)

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  8. swift学习笔记4——扩展、协议

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  9. swift学习笔记3——类、结构体、枚举

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  10. swift学习笔记2——函数、闭包

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

随机推荐

  1. vmvare如何安装xp虚拟机

    http://jingyan.baidu.com/article/a681b0ded8e25e3b19434640.html 一直以来,许多的朋友都不熟悉怎么安装在虚拟机上装windows系统 200 ...

  2. 30.QT IDE编写

    mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTe ...

  3. 1. Two Sum[E]两数之和

    题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  4. python之路——装饰器函数

    阅读目录 楔子 装饰器的形成过程 开放封闭原则 谈装饰器主要功能和装饰器固定结构 带参数的装饰器 多个装饰器装饰一个函数 返回顶部 楔子 作为一个会写函数的python开发,我们从今天开始要去公司上班 ...

  5. 爬虫概念与编程学习之如何爬取视频网站页面(用HttpClient)(二)

    先看,前一期博客,理清好思路. 爬虫概念与编程学习之如何爬取网页源代码(一) 不多说,直接上代码. 编写代码 运行 <!DOCTYPE html><html><head& ...

  6. 关于requestAnimationFrame()和cancelAnimationFrame()与定时器之间的比较

    在渲染页面动画的时候,其实也没有必要用定时器(setInterval),其实requestAnimationFrame()和cancelAnimationFrame()也能达到相应的效果,他是HTML ...

  7. Python 之 PyCharm使用

    PyCharm  的官方网站地址是:https://www.jetbrains.com/pycharm/download/ 教育版:https://www.jetbrains.com/pycharm- ...

  8. ZBrush2018中文版全球同步发售,终身授权

    ZBrush 2018于2018.3.28发布了!这个我们期待已久的2018新版本等了几年,它终于来了! 不负众望,ZBrush 2018的这一次更新,简直炸裂,新功能真是太好用了!2018版是ZBr ...

  9. 使用node+mysql进行后端开发

    使用koa: koa2是一个类,所以引入koa后,要创建实例化“对象”,才能使用koa内部封装的方法. 设置监听端口: 处理http请求: 1.http请求处理链 A.通过app.use()注册asy ...

  10. spring使用注解开发

    1.准备工作(1)导入common-annotations.jar(2)导入schema文件 文件名为spring-context-2.5.xsd(3)在xml的beans节点中配置 service层 ...