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. CorePlot学习六---点击scatterPlot中的symbol点时弹出对应的凝视

    因为项目须要用到用户点击 symbol时,弹出对应的具体信息,发现国内解说的比較少,经过一番搜索验证最终解决,先看效果图: 详细须要改动的代码例如以下: 首先要引用托付方法:CPTScatterPlo ...

  2. java匿名内部类的使用注意事项

    1.首先匿名内部类要继承自抽象基类或者实现基类接口 like this abstract class Seed{ int cnt; public Seed(int x){ cnt=x; } abstr ...

  3. 12.红黑树set

    #include <iostream> //红黑树(自动保证平衡,自动生成平衡查找树) #include <set> #include <cstring> #inc ...

  4. Python 从入门到精通 全程最佳实现梳理

    零零星星的时间,持续完善中...... 1.一些基础的必要信息归纳 Python 官网 www.python.org 发明者 吉多·范罗苏姆 发行时间 1991年,​26年前 编程泛型 多泛型.面向对 ...

  5. HD-ACM算法专攻系列(3)——Least Common Multiple

    题目描述: 源码: /**/ #include"iostream" using namespace std; int MinComMultiple(int n, int m) { ...

  6. SharePoint 修改完或制作完一定要发布

    设置了匿名访问但是网站就是需要登录,找了很多问题. 首先想到的映射问题,然后努力检查,最后把代码删掉,然后把站删掉,最后测试出来问题. 点击上方[网站设置] 把修改过的文件发布. 母版也和布局页 一定 ...

  7. Css border样式

    1 四个边框 border-left 设置左边框,一般单独设置左边框样式使用border-right 设置右边框,一般单独设置右边框样式使用border-top 设置上边框,一般单独设置上边框样式使用 ...

  8. codeforces 544 D Destroying Roads 【最短路】

    题意:给出n个点,m条边权为1的无向边,破坏最多的道路,使得从s1到t1,s2到t2的距离不超过d1,d2 因为最后s1,t1是连通的,且要破坏掉最多的道路,那么就是求s1到t1之间的最短路 用bfs ...

  9. HDU 1240 Asteroids!【BFS】

    题意:给出一个三维的空间,给出起点和终点,问是否能够到达终点 和上一题一样,只不过这一题的坐标是zxy输入的, 因为题目中说的是接下来的n行中分别是由n*n的矩形组成的,所以第一个n该是Z坐标,n*n ...

  10. CSS的flex布局和Grid布局

    一.什么是 flex 布局 2009年,W3C 提出了一种新的方案----Flex 布局,可以简便.完整.响应式地实现各种页面布局.目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这 ...