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)--控制流的更多相关文章

  1. swift学习笔记之控制流

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

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

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

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

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

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

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

  5. Swift学习笔记一

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

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

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

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

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

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

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

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

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

随机推荐

  1. 在独立的文件里定义WPF资源

    一.文章概述 本演示介绍怎样在单独的文件里定义WPF资源.并在须要的地方调用相关资源文件. 相关下载(代码.屏幕录像):http://pan.baidu.com/s/1sjO7StB 在线播放:htt ...

  2. mysql-面试题目1

    一.数据库的ACID 原子性(Atomicity):保证事务中的所有操作全部执行或全部不执行. 一致性(Consistency):保证数据库始终保持数据的一致性——事务操作之前和之后都是一致的. 隔离 ...

  3. html-上左右布局方式---ShinePans

    文件包括 main.html  top.html  left.html  childhood.html  moonsong.html  herethesea.html 主要布局效果: 代码: main ...

  4. 关于oracle db 11gR2版本号上的_external_scn_rejection_threshold_hours參数和scn headroom补丁问题

    关于oracle db 11gR2版本号上的_external_scn_rejection_threshold_hours參数和scn headroom补丁问题 来自于: Installing, Ex ...

  5. Sqlite3的安装Windows

  6. matplotlib 可视化 —— 定制 matplotlib

    1. matplotlibrc 文件 matplotlib使用matplotlibrc [matplotlib resource configurations] 配置文件来自定义各种属性,我们称之为 ...

  7. sql查询每个学生的最高成绩mysql语句

    张三 语文 100 张三 数学 83 李四 语文 88 李四 数学 100 查询每个学生的最高成绩. select b.* from (select name,max(score) score fro ...

  8. js之insertBefore(newElement,oldElement)

    insertBefore的用法,以及注意事项,并且模仿编写insertAfter()方法 DOM提供的一个名为insertBefore()的方法,用来将一个新元素插入到现有的元素的前面. 使用这个方法 ...

  9. ASP.NET 部分视图

    ASP.NET MVC 里的部分视图,相当于 Web Form 里的 User Control.我们的页面往往会有许多重用的地方,可以进行封装重用. 使用部分视图有以下优点:   1. 可以简写代码. ...

  10. Nordic Collegiate Programming Contest 2015​(第七场)

    A:Adjoin the Networks One day your boss explains to you that he has a bunch of computer networks tha ...