Swift学习笔记(7)--控制流
1.For循环
//1.条件递增
for var index = 0; index < 3; ++index {
println("index is \(index)")
} //2.for in循环
// 2.1有变量名
for index in 1...5 {
println("\(index) times 5 is \(index * 5)")
} // 2.2没有变量名,如果不需要知道区间内每一项的值,你可以使用下划线(_)替代变量名来忽略对值的访问:
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
} // 2.3遍历数组
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
println("Hello, \(name)!")
} // 2.4遍历字典
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
println("\(animalName)s have \(legCount) legs")
} // 2.5遍历字符串的字符
for character in "Hello" {
println(character)
}
2.While循环
//1.while循环
let maxValue = 100
var x = 0
while x < maxValue {
x++
}
println(x) //100 //2.do while循环
var y = 0
do {
y++
} while y < maxValue
println(y) //100
3.If语句
var checkValue = 10
//1.if
var x = 1
if x < checkValue {
println("x < 10")
} //2.if else
var y = 15
if y < checkValue {
println("y < checkValue")
} else {
println("y >= checkValue")
} //3.elseif
var z = 10
if z < checkValue {
println("z < checkValue")
} else if z > checkValue {
println("z > checkValue")
} else {
println("z = checkValue")
}
4.Switch语句
与 C 语言和 Objective-C 中的switch语句不同,在 Swift 中,当匹配的 case 分支中的代码执行完毕后,程序会终止switch语句,而不会继续执行下一个 case 分支。这也就是说,不需要在 case 分支中显式地使用break语句。这使得switch语句更安全、更易用,也避免了因忘记写break语句而产生的错误。
//1.普通匹配
let someCharacter: Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
println("\(someCharacter) is a vowel")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
println("\(someCharacter) is a consonant")
default:
println("\(someCharacter) is not a vowel or a consonant")
}// 输出 "e is a vowel" //2.区间匹配
let count = 200
var naturalCount: String
switch count {
case 0:
naturalCount = "0"
case 1...10:
naturalCount = "a few"
case 10...100:
naturalCount = "several"
case 100...999:
naturalCount = "hundreds of"
case 1000...999_999:
naturalCount = "thousands of"
default:
naturalCount = "millions and millions of"
}
println("There are \(naturalCount).")// 输出 "There are hundreds of." //3.(Tuple)元组匹配可以使用元组在同一个switch语句中测试多个值。元组中的元素可以是值,也可以是区间。另外,使用下划线(_)来匹配所有可能的值。
// 以2*2的坐标区间为例
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
println("(0, 0)在原点")
case (_, 0):
println("(\(somePoint.0), 0)在x轴")
case (0, _):
println("(0, \(somePoint.1))在y轴")
case (-2...2, -2...2):
println("(\(somePoint.0), \(somePoint.1))在2*2区域内")
default:
println("(\(somePoint.0), \(somePoint.1))在2*2区域外")
}// 输出 "(1, 1)在2*2区域内" //值绑定:可以将元组中的值临时绑定到一个变量中
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
println("在x轴,且x坐标值为:\(x)")
case (0, let y):
println("在y轴,且y坐标值为:\(y)")
case let (x, y):
println("坐标点:(\(x), \(y))")
}// 输出 "在x轴,且x坐标值为:2" //case 分支的模式可以使用where语句来判断额外的条件。
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
println("(\(x), \(y)) 在x == y的直线上")
case let (x, y) where x == -y:
println("(\(x), \(y)) 在x == -y的直线上")
case let (x, y):
println("(\(x), \(y)) 不在对称线上")
}// 输出 "(1, -1) 在x == -y的直线上"
5.控制转移关键字
- continue
- break
- fallthrough (贯穿)
- return (在函数中使用,略)
//1.continue
for i in 1...10 {
if i%2 == 0 {
continue //整除2就直接进入下一次循环
}
print("\(i) ")
} // 输出1 3 5 7 9 //2.break
for i in 1...10 {
if i%3 == 0 {
break //整除3就直接跳出for循环
}
print("\(i) ")
} // 输出1 2
println("") //3.fallthrough 贯穿
/*Swift 中的switch不会从上一个 case 分支落入到下一个 case 分支中。相反,只要第一个匹配到的 case 分支完成了它需要执行的语句,整个switch代码块完成了它的执行。相比之下,C 语言要求你显示的插入break语句到每个switch分支的末尾来阻止自动落入到下一个 case 分支中。Swift 的这种避免默认落入到下一个分支中的特性意味着它的switch 功能要比 C 语言的更加清晰和可预测,可以避免无意识地执行多个 case 分支从而引发的错误。 如果你确实需要 C 风格的贯穿(fallthrough)的特性,你可以在每个需要该特性的 case 分支中使用fallthrough关键字。下面的例子使用fallthrough来创建一个数字的描述语句。*/
let integerToDescribe = 5
var description = "数字 \(integerToDescribe) 是"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += "一个质数, 也是一个"
fallthrough
case 1, 3, 5, 7, 9, 11, 13, 15, 17, 19:
description += "奇数, 也是一个"
fallthrough
default:
description += "整数"
}
println(description)
// 输出 "数字 5 是一个质数, 也是一个奇数, 也是一个整数."
6.带标签控制流
在 Swift 中,你可以在循环体和switch代码块中嵌套循环体和switch代码块来创造复杂的控制流结构。然而,循环体和switch代码块两者都可以使用break语句来提前结束整个方法体。因此,显示地指明break语句想要终止的是哪个循环体或者switch代码块,会很有用。类似地,如果你有许多嵌套的循环体,显示指明continue语句想要影响哪一个循环体也会非常有用。
为了实现这个目的,你可以使用标签来标记一个循环体或者switch代码块,当使用break或者continue时,带上这个标签,可以控制该标签代表对象的中断或者执行。
let maxValue = 100
let flagValue = 55
let stepValue = 10
var i = 5 addLoop: while i < maxValue {
i = i + stepValue
switch i {
case flagValue:
println("到达标示的数字,退出wile循环")
break addLoop
case 1...10:
println("数字\(i)介于1到10之间")
case 11...20:
println("数字\(i)介于11到20之间")
case 21...30:
println("数字\(i)介于21到30之间")
case 31...40:
println("数字\(i)介于31到40之间")
case 41...50:
println("数字\(i)介于41到50之间")
case 51...60:
println("数字\(i)介于51到60之间") default:
println("Default")
}
}
Swift学习笔记(7)--控制流的更多相关文章
- swift学习笔记之控制流
控制流: 1.if语句 let count = { print("yes") }else{ print("no") } 2.switch语句 (1)Swift中 ...
- 【swift学习笔记】二.页面转跳数据回传
上一篇我们介绍了页面转跳:[swift学习笔记]一.页面转跳的条件判断和传值 这一篇说一下如何把数据回传回父页面,如下图所示,这个例子很简单,只是把传过去的数据加上了"回传"两个字 ...
- Swift学习笔记(一)搭配环境以及代码运行成功
原文:Swift学习笔记(一)搭配环境以及代码运行成功 1.Swift是啥? 百度去!度娘告诉你它是苹果最新推出的编程语言,比c,c++,objc要高效简单.能够开发ios,mac相关的app哦!是苹 ...
- swift学习笔记1——基础部分
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
- Swift学习笔记一
最近计划把Swift语言系统学习一下,然后将MagViewer用这种新语言重构一次,并且优化一下,这里记录一下Swift的学习笔记. Swift和Objective-C相比,在语法和书写形式上做了很多 ...
- swift学习笔记5——其它部分(自动引用计数、错误处理、泛型...)
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
- swift学习笔记4——扩展、协议
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
- swift学习笔记3——类、结构体、枚举
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
- swift学习笔记2——函数、闭包
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
随机推荐
- SharePoint Search之(七)Search result- 结果源
在使用搜索引擎的时候.非常多情况下,用户希望限定一下搜索范围,以便更加easy找到想要的结果. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvU1BGYXJ ...
- jquery开发之代码风格
1,链式操作风格. (1) 对于同一个对象不超过三个操作的.可直接写成一行.代码例如以下: $("li").show().unbind("click"); (2 ...
- taglist安装
注意:taglist依赖于ctags,所以要先装ctags,否则taglist装了也没法用!1.首先安装ctags1)ubuntu安装sudo apt-get install exuberant-ct ...
- springboot 注入xml自定义类
新建入口类可扫描类: @Configuration @ImportResource(locations = {"classpath:spring-bean.xml"}) publi ...
- Metasploit的三种启动方式
不多说,直接上干货! 注意:博主我用的是Kali linux 2016.2(Rolling). msfcli 的启动 root@kali:~# msfcli -h bash: msfcli: 未找到 ...
- java9新特性-2-安装与官网说明
1.jdk 9的下载 http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html 下载安 ...
- Android控件postDelayed用法,View自带的定时器
有一个需求是这样的,点击加关注按钮后,执行关注操作,成功后按钮文字变为“已关注”,保持3秒,三秒后按钮文字便问“取消关注”,点击后执行取消关注的操作 源码: public boolean postDe ...
- 运维派 企业面试题3 为上题中的 "十个随机字母_test.html" 文件 更名
Linux运维必会的实战编程笔试题(19题) 企业面试题3 #将试题2中创建的文件名uopiyhgawe_test.html# test-->修改为omg,html-->HTML 方法一: ...
- 24 款必备的 Linux 桌面应用(2016 版)
作者: Munif Tanjim 译者: LCTT GHLandy | 2016-12-21 08:41 评论: 41 收藏: 13 摘要:Linux 的必备软件有哪些?这将会是一个非常主观的回答 ...
- [NOIP2016普及组]魔法阵
题目:洛谷P2119.Vijos P2012.codevs5624. 题目大意:有n件物品,每件物品有个魔法值.要求组成魔法阵(Xa,Xb,Xc,Xd),该魔法阵要满足Xa<Xb<Xc&l ...