swift基础之流程控制

・Swift 流程控制简介

・Swift提供了类似 C 语言的流程控制结构,包括可以多次执行任务的for和while循环,

基于特定条件选择执行不同代码分支的if和switch语句,还有控制流程跳转到其他代码

的break和continue语句。

・除了 C 语言里面传统的for条件递增(for-condition-increment)循环,Swift 还增

加了for-in循环,用来更简单地遍历数组(array),字典(dictionary),区间

(range),字符串(string)和其他序列类型。

・Swift 的switch语句比 C 语言中的更强大。在 C 语言中,如果某个 case 不小心漏

写了break,这个 case 就会贯穿(fallthrough)至下一个 case,Swift 无需写break,

所以不会发生这种贯穿的情况。case 还可以匹配更多的类型模式,包括区间匹配

(range matching),元组(tuple)和特定类型。

 

・For循环

・for循环用来按照指定的次数多次执行一系列语句

・Swift 提供两种for循环形式:for-in、for条件递增

・for条件递增(for-condition-increment)

・用来重复执行一系列语句直到达成特定条件,一般通过在每次循环完成后增加计数

器的值来实现

・for-in

・for-in用来遍历一个区间范围(range),序列(sequence),集合

(collection),系列(progression)里面所有的元素

・如果不需要知道区间内每一项的值,你可以使用下划线(_)替代变量名来忽略对

值的访问

 

・While循环

・Swift 提供两种while循环形式:while、do-while

・while

・先判断条件,再执行

・do-while

・先执行一次,再判断条件

 

・条件语句

・Swift 提供两种类型的条件语句:if语句和switch语句

・当条件较为简单且可能的情况很少时,使用if语句。而switch语句更适用于条件较

复杂、可能情况较多且需要用到模式匹配(pattern-matching)的情况

・If

・if语句最简单的形式就是只包含一个条件,当且仅当该条件为true时,才执行相关

代码,当条件为false时,执行 else 语句

let今天天⽓ہ好 =true

if今天天⽓ہ好 {

println("我们就去爬⼭ׇ")

}else {

println("在教室学习")

}

 

・Switch

・switch语句会尝试把某个值与若干个模式(pattern)进行匹配。根据第一个匹配成

功的模式,switch语句会执行对应的代码。

・当有可能的情况较多时,通常用switch语句替换if语句

・switch语句都由多个 case 构成,每一个 case 都是代码执行的一条分支

・在某些不可能涵盖所有值的情况下,你可以使用默认(default)分支满足该要求,这

个默认分支必须在switch语句的最后面

let charA:Character ="A"

switchcharA {

case"a","A":  //如果想匹配多个条件,可以在⼀Ӡ个case⾥ज⾯ॶ把多个条件⽤ݒ(,)隔开

println("The letter a")//每⼀Ӡ个 case分⽀ْ都必须包含⾄ࠟ少⼀Ӡ条语句

case"A":

println("The letter A")

default:

println("default")

}

 

・不存在隐式的贯穿(No Implicit Fallthrough)

・Swift 中的switch,当匹配的 case 分支中的代码执行完毕后,程序会终止switch语

句,而不会继续执行下一个 case 分支。这也就是说,不需要在 case 分支中显式地使

用break语句

・每个 case 分支都必须包含至少一条语句;如果想匹配多个条件,可以在一个case里

面把多个条件用逗号隔开

//每⼀Ӡ个 case分⽀ْ都必须包含⾄ࠟ少⼀Ӡ条语句;如果想匹配多个条件,可以在⼀Ӡ个case⾥ज⾯ॶ把多个条件⽤ݒ

(,)隔开

let anotherCharacter:Character ="a"

switchanotherCharacter {

case"a":  //编译报错

case"A":

println("The letter A")

default:

println("Not the letter A")

}

 

・区间范围匹配(Range Matching)

・case 分支的模式也可以是一个值的范围

//使⽤ݒ范围匹配来输出任意数字对应的⾃ࠞ然语⾔ࢠ格式

let count =3_000_000_000_000

let countedThings ="stars in the Milky Way"

var naturalCount:String

switchcount {

case0:

naturalCount ="no"

case1...3:

naturalCount ="a few"

case4...9:

naturalCount ="several"

case10...99:

naturalCount ="tens of"

case100...999:

naturalCount ="hundreds of"

case1000...999_999:

naturalCount ="thousands of"

default:

naturalCount ="millions and millions of"

}

println("There are\(naturalCount)\(countedThings).")

 

・匹配元组Tuple

・可以使用元组在同一个switch语句中测试多个值。元组中的元素可以是值,也可以是

范围。另外,使用下划线(_)来匹配所有可能的值

・Swift 允许多个 case 匹配同一个值(C语言不支持)。如果存在多个匹配,只会执行第

一个被匹配到的 case 分支

//使⽤ݒ⼀Ӡ个(Int, Int)类型的元组来分类下图中的点(x, y)

let somePoint = (1,1)

switchsomePoint {

case (0,0):

println("(0, 0) is at the origin")

case (_,0):

println("(\(somePoint.0), 0) is on the x-axis")

case (0,_):

println("(0,\(somePoint.1)) is on the y-axis")

case (-2...2, -2...2):

println("(\(somePoint.0),\(somePoint.1)) is inside the box")

default:

println("(\(somePoint.0),\(somePoint.1)) is outside of the box")

}

 

・值绑定(Value Bindings)

・case 分支允许将匹配的值绑定到一个临时的常量或变量,这些常量或变量在该 case

分支里就可以被引用了——这种行为被称为值绑定(value binding)

//在⼀Ӡ个(Int, Int)类型的元组中使⽤ݒ值绑定来分类下图中的点(x, y)

let anotherPoint = (2,0)

switchanotherPoint {

case (let x,0):    //此时x只是⼀Ӡ个占位符,⽤ݒ来临时的获取switch条件中的⼀Ӡ个或多个值

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)")

caselet (x, y):

println("somewhere else at (\(x),\(y))")

}

 

・Where 附加条件

・case 分支可以使用where语句来判断额外的条件

//case分⽀ْ可以使⽤ݒwhere语句来判断额外的条件

let yetAnotherPoint = (1, -1)

switchyetAnotherPoint {

caselet (x, y)where x == y:  //把值赋给x,y,并且要求x等于y

println("(\(x),\(y)) is on the line x == y")

caselet (x, y)where x == -y:

println("(\(x),\(y)) is on the line x == -y")

caselet (x, y):

println("(\(x),\(y)) is just some arbitrary point")

}

 

・控制传递语句(Control Transfer Statements)

・控制转移语句改变你代码的执行顺序,通过它你可以实现代码的跳转

・Swift有四种控制转移语句:

・continue

・break

・fallthrough

・return

 

・Continue

・continue语句告诉一个循环体立刻停止本次循环迭代,重新开始下次循环迭代

//continue语句告诉⼀Ӡ个循环体⽴ެ刻停⽌ڶ本次循环迭代,重新开始下次循环迭代。就好

像在说“本次循环迭代我已经执⾏ࢄ完了,执⾏ࢄ下⼀Ӡ次吧”

let puzzleInput ="great minds think alike"

var puzzleOutput =""

for characterinpuzzleInput {

switch character {

case"a","e","i","o","u"," ":

continue

default:

puzzleOutput += character

    }

}

println(puzzleOutput)

 

・Break

・break语句会立刻结束整个控制流的执行。当你想要更早的结束一个switch代码块或

者一个循环体时,可以使用break语句

・循环语句中的 break

・当在一个循环体中使用break时,会立刻中断该循环体的执行,然后跳转到表示循环

体结束的大括号(})后的第一行代码。不会再有本次循环迭代的代码被执行,也不会再有

下次的循环迭代产生。

・Switch 语句中的 break

・当在一个switch代码块中使用break时,会立即中断该switch代码块的执行,并且跳

转到表示switch代码块结束的大括号(})后的第一行代码。

 

・标签语句(Labeled Statements)

・可以使用标签来标记一个循环体或者switch代码块,当使用break或者continue时,

带上这个标签,可以控制该标签代表对象的中断或者执行,适合复杂的控制流结构

//根据分数评等级,超过100分跳过,遇到负数停⽌ڶ循环

var score = [96,83,43,101,66,70, -5,99]

!

First:for sinscore {    //定义标签First

switch s/10 {

case10:

continue First  //使⽤ݒ标签

case9:

println("\(s)分为优秀")

case8:

println("\(s)分为良好")

case6...7:

println("\(s)分为中等")

case0:

break First    //使⽤ݒ标签,终⽌ڶfor循环。如果这⾥ज没使⽤ݒ标签,break的将是switch

default:

println("\(s)分为没及格")

    }

}

posted @ 2015-10-14 10:49  -heart-heartless*  阅读(374)  评论(0编辑  收藏  举报