Control Flow(控制流)
Swift 提供了类C语言的所有控制流结构.包括循环结构:
for和
while;选择支结构:
if与
switch.和一些让你业务流程转向的关键字:
break,
continue等...
除了传统的
for-condition-increment循环外,Swift还新增
for-in循环结构,使得在Arrays,Dictionaries,Ranges,Strings以及其他的sequences能更好更方便的迭代,而不在像以前写一篇迭代代码.现在只需要一行短小的代码即可搞定.当然该for-in就相当于Java中的For(增强for循环).
- For Loops(for循环)
Swift提供2种for循环:
for-in 用于迭代range,sequence,collection or progression.的每一项.
for-condition-increment执行一条语句使得condition条件达到才结束,通过increment计数来结束循环
For-In
使用for-in循环来迭代:
ranges://迭代一个范围
for-in遍历ranges的原理是把1...5得到的数1,2,3,4,5依次载入index中,直到index为5,那么终止循环.for index in 1...5{ <span style="white-space:pre"> </span>println("\(index)time 5 is \(index*5)") }
Note:这里我们需要主要,这个index是一个常量,它的值是循环内自动赋予.它不需要定义就可以使用.因为是在循环体里隐式定义的.不需要主动申明let关键字
如果我们不需要这个index,我们忽略掉这个值可以使用一个下划线"_"来替代这个index.(这种方式我第 一次是在go语言中见到,第一次看到就喜欢上了这种写法.真的很好)
Arrays://迭代一个数组let base = 3 let power = 10 var answer = 1 for _ in 1...power {//这里我们不需要在乎每次得到range值.我们只在意每次循环的过程,使用_来忽略它. answer *= base } println("\(base) to the power of \(power) is \(answer)") // prints "3 to the power of 10 is 59049"
Dictionary://迭代一个字典let names = ["Anna", "Alex", "Brian", "Jack"] for name in names { println("Hello, \(name)!") } // Hello, Anna! // Hello, Alex! // Hello, Brian! // Hello, Jack!
除Array和Dictionary之外,你还可以使用for-in来遍历String的每一个Characterlet numberOfLegs = ["spider": 8, "ant": 6, "cat": 4] for (animalName, legCount) in numberOfLegs { println("\(animalName)s have \(legCount) legs") } // spiders have 8 legs // ants have 6 legs // cats have 4 legs
String://迭代字符串
for character in "Hello" { println(character) } // H // e // l // l // o
- For-Condition-Increment
除for-in循环外,swift还支持带有条件以及增量器类似C语言的for循环:
语法为:for var index = 0; index < 3; ++index { println("index is \(index)") } // index is 0 // index is 1 // index is 2
for initialization ; condition ; increment { statements }
该循环遵照一下规则:
1:当第一次进入循环时,initialization表达式会且只执行一次,定义一个循环类要使用的变量或者常量.
2:条件表达式Condition会在循环开始进行一次计算,如果计算结果为true那么继续执行,如果为false直接跳出循环.
3:当循环体代码执行完毕后,计数器开始被执行,赋予初始化的变量新值,然后再进行步骤2,检查条件是否满足.
- While以及Do-While
这2种循环就不再多提了,大多数语言都有的.与For循环规则一致:
While语法:
Do-While语法:while condition { statements }
do { statements } while condition
- Conditional Statements(条件语句)
If 条件语句
该语句不是很难也没加什么新东西,就不再多讲,我们主要关注下Switch条件语句.
Switch条件语句
Swift语言提供的Switch相比其他语言Java,C#相比,Switch语言更简洁,首先让程序员不用显示调用break来直接跳出Switch块,其次,一个Case可以包含多个条件用逗号隔开.最好的一个功能是提供自定义参数来获取switch块条件的值,这个特性我在我所学过的语言是没看到的.
Syntax:
Simple example:switch some value to consider { case value 1 : respond to value 1 case value 2 , value 3 : respond to value 2 or 3 default: otherwise, do something else }
在这个例子中,我们看到2个比较新的东西,let name = "shoper" switch(name){ case "shoper","shawn": println("Shoper or Shawn") case "chloe": println("Chloe") default: println("not at all") } //print "Shoper or Shawn" and not //print "Shoper or Shawn //Chloe"
1.支持多条件,只要满足一个条件即可进入case块(Note:如果是数字也可以使用Ranges替代)
2.不需要现式申明break了(这种特性在Go语言已经存在),当执行完case块中代码自动个跳出switch语句.如果需要进入下一个case块,加上fallthough即可
Value Bindings(值绑定)
Swift在Switch条件语句中提供了值绑定功能,在case块中能绑定符合条件的临时变量或者常量,但是只能用于当前的case块种,这就是所谓的值绑定.
Simple Example:
附加条件: where//我们可以使用再添加附加条件来再次筛选
Swift在Switch条件语句中提供了值绑定功能,在case块中能绑定符合条件的临时变量或者常量,但是只能用于当前的case块种,这就是所谓的值绑定.
Simple Example:
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
println("on the x-axis with an x value of \(x)")
case (0, let y):
println("on the y-axis with a y value of \(y)")
case let (x, y):
println("somewhere else at (\(x), \(y))")
}
// prints "on the x-axis with an x value of 2"
附加条件: where//我们可以使用再添加附加条件来再次筛选
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
println("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
println("(\(x), \(y)) is on the line x == -y")
case let (x, y):
println("(\(x), \(y)) is just some arbitrary point")
}
// prints "(1, -1) is on the line x == -y"
- Control Transfer Statements(控制转移语句)
- Labeled Statements(标签语句)