Swift提供了所有C語言中相似的控制流結(jié)構(gòu)。包括for和while循環(huán);if和switch條件語句;break和continue跳轉(zhuǎn)語句等。
Swift還加入了for-in循環(huán)語句,讓編程人員可以在遍歷數(shù)組,字典,范圍,字符串或者其它序列時(shí)更加便捷。
相對(duì)于C語言,Swift中switch語句的case語句后,不會(huì)自動(dòng)跳轉(zhuǎn)到下一個(gè)語句,這樣就避免了C語言中因?yàn)橥沚reak而造成的錯(cuò)誤。另外case語句可以匹配多種類型,包括數(shù)據(jù)范圍,元組,或者特定的類型等。switch語句中已匹配的數(shù)值也可以被用在后續(xù)的case語句體中,where關(guān)鍵詞還能被加入任意的case語句中,來增加匹配的方式。
1、for循環(huán)
for循環(huán)可以根據(jù)設(shè)置,重復(fù)執(zhí)行一個(gè)代碼塊多次。Swift中提供了兩種for循環(huán)方式:
for-in循環(huán),對(duì)于數(shù)據(jù)范圍,序列,集合等中的每一個(gè)元素,都執(zhí)行一次
for-condition-increment,一直執(zhí)行,知道一個(gè)特定的條件滿足,每一次循環(huán)執(zhí)行,都會(huì)增加一次計(jì)數(shù)
for-in循環(huán)
下面的例子打印出了5的倍數(shù)序列的前5項(xiàng)
for index in 1...5 {
println("/(index) times 5 is /(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
迭代的項(xiàng)目是一個(gè)數(shù)字序列,從1到5的閉區(qū)間,通過使用(…)來表示序列。index被賦值為1,然后執(zhí)行循環(huán)體中的代碼。在這種情況下,循環(huán)只有一條語句,也就是打印5的index倍數(shù)。在這條語句執(zhí)行完畢后,index的值被更新為序列中的下一個(gè)數(shù)值2,println函數(shù)再次被調(diào)用,一次循環(huán)直到這個(gè)序列的結(jié)尾。
在上面的例子中,index在每一次循環(huán)開始前都已經(jīng)被賦值,因此不需要在每次使用前對(duì)它進(jìn)行定義。每次它都隱式地被定義,就像是使用了let關(guān)鍵詞一樣。注意index是一個(gè)常量。
注意:index只在循環(huán)中存在,在循環(huán)完成之后如果需要繼續(xù)使用,需要重新定義才可以。
如果你不需要序列中的每一個(gè)值,可以使用_來忽略它,僅僅只是使用循環(huán)體本身:
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
}
println("/(base) to the power of /(power) is /(answer)")
// prints "3 to the power of 10 is 59049"
這個(gè)例子計(jì)算了一個(gè)數(shù)的特定次方(在這個(gè)例子中是3的10次方)。連續(xù)的乘法從1(實(shí)際上是3的0次方)開始,依次累乘以3,由于使用的是半閉區(qū)間,從0開始到9的左閉右開區(qū)間,所以是執(zhí)行10次。在循環(huán)的時(shí)候不需要知道實(shí)際執(zhí)行到第一次了,而是要保證執(zhí)行了正確的次數(shù),因此這里不需要index的值。
同理我們可以使用for-in來循環(huán)遍歷一個(gè)數(shù)組的元素
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
println("Hello, /(name)!")
}
// Hello, Anna!
// Hello, Alex!
// Hello, Brian!
// Hello, Jack!
在遍歷字典的時(shí)候,可以使用key-value對(duì)來進(jìn)行遍歷。每一個(gè)字典中的元素都是一個(gè)(key, value)元組,當(dāng)遍歷的時(shí)候,可以指定字段的key和value為一個(gè)特定的名稱,這樣在遍歷的時(shí)候就可以更好地理解和使用它們,比如下面例子中的animalName和legCount:
let 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
字典中的元素在遍歷的時(shí)候一般不需要按照插入的順序,因此不能保證遍歷字典的時(shí)候,元素是有序的。更多跟數(shù)組和字典相關(guān)的內(nèi)容可以參考:Collection Types
另外在數(shù)組和字典中也可以使用類似的遍歷方式,如可以使用for-in循環(huán)來遍歷字符串中的每一個(gè)字符:
for character in "Hello" {
println(character)
}
// H
// e
// l
// l
// o
For-Condition-Increment條件循環(huán)
Swift同樣支持C語言樣式的for循環(huán),它也包括了一個(gè)條件語句和一個(gè)增量語句:
for var index = 0; index < 3; ++index {
println("index is /(index)")
}
// index is 0
// index is 1
// index is 2
下面是這種for循環(huán)的一般結(jié)構(gòu):
for initialization; condition; increment {
statements
}
分號(hào)在這里用來分隔for循環(huán)的三個(gè)結(jié)構(gòu),和C語言一樣,但是不需要用括號(hào)來包裹它們。
這種for循環(huán)的執(zhí)行方式是:
1、當(dāng)進(jìn)入循環(huán)的時(shí)候,初始化語句首先被執(zhí)行,設(shè)定好循環(huán)需要的變量或常量
2、測(cè)試條件語句,看是否滿足繼續(xù)循環(huán)的條件,只有在條件語句是true的時(shí)候才會(huì)繼續(xù)執(zhí)行,如果是false則會(huì)停止循環(huán)。
3、在所有的循環(huán)體語句執(zhí)行完畢后,增量語句執(zhí)行,可能是對(duì)計(jì)數(shù)器的增加或者是減少,或者是其它的一些語句。然后返回步驟2繼續(xù)執(zhí)行。
這種循環(huán)方式還可以被描述為下面的形式:
initialization
while condition {
statements
increment
}
在初始化語句中被定義(比如var index = 0)的常量和變量,只在for循環(huán)語句范圍內(nèi)有效。如果想要在循環(huán)執(zhí)行之后繼續(xù)使用,需要在循環(huán)開始之前就定義好:
var index: Int
for index = 0; index < 3; ++index {
println("index is /(index)")
}
// index is 0
// index is 1
// index is 2
println("The loop statements were executed /(index) times")
// prints "The loop statements were executed 3 times"
需要注意的是,在循環(huán)執(zhí)行完畢之后,index的值是3,而不是2。因?yàn)槭窃趇ndex增1之后,條件語句index < 3返回false,循環(huán)才終止,而這時(shí),index已經(jīng)為3了。
2、while循環(huán)
while循環(huán)執(zhí)行一系列代碼塊,直到某個(gè)條件為false為止。這種循環(huán)最長(zhǎng)用于循環(huán)的次數(shù)不確定的情況。Swift提供了兩種while循環(huán)方式:
while循環(huán),在每次循環(huán)開始前測(cè)試循環(huán)條件是否成立
do-while循環(huán),在每次循環(huán)之后測(cè)試循環(huán)條件是否成立
while循環(huán)
while循環(huán)由一個(gè)條件語句開始,如果條件語句為true,一直執(zhí)行,直到條件語句變?yōu)閒alse。下面是一個(gè)while循環(huán)的一般形式:
while condition {
statements
}
下面的例子是一個(gè)簡(jiǎn)單的游戲,Snakes and Ladders,蛇和梯子:
游戲的規(guī)則是這樣的:
游戲面板上有25個(gè)格子,游戲的目標(biāo)是到達(dá)第25個(gè)格子;
每個(gè)回合通過一個(gè)6面的骰子來決定行走的步數(shù),行走的路線按右圖所示;
如果落在梯子的底部,那么就爬上那個(gè)梯子到達(dá)另外一個(gè)格子;
如果落到蛇的頭部,就會(huì)滑到蛇尾部所在的格子。
游戲面板由一個(gè)Int數(shù)組組成,大小由一個(gè)常量設(shè)置finalSquare,同時(shí)用來檢測(cè)是否到達(dá)了勝利的格子。游戲面板由26個(gè)Int數(shù)字0初始化(不是25個(gè),因?yàn)閺?到25有26個(gè)數(shù)字)
let finalSquare = 25
var board = Int[](count: finalSquare + 1, repeatedValue: 0)
其中一些格子被設(shè)置為一些特定的值用來表示蛇或者梯子。有梯子的地方是整數(shù),而有蛇的地方是負(fù)數(shù):
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
第三個(gè)格子是一個(gè)梯子的底部,表示玩家可以通過梯子到達(dá)第11格,因此設(shè)置board[3]為+08,表示前進(jìn)8步。同理蛇的位置設(shè)置為負(fù)數(shù),表示后退i步。
玩家從為0的格子開始游戲。
var square = 0
var diceRoll = 0
while square < finalSquare {
// roll the dice
if ++diceRoll == 7 { diceRoll = 1 }
// move by the rolled amount
square += diceRoll
if square < board.count {
// if we're still on the board, move up or down for a snake or a ladder
square += board[square]
}
}
println("Game over!")
這個(gè)例子用到了一個(gè)非常簡(jiǎn)單的擲骰子的方式,就是每次加1,而不是使用一個(gè)隨機(jī)數(shù)。diceRoll用來表示每次行走的步數(shù),需要注意的是,每次執(zhí)行前,++diceRoll都會(huì)先執(zhí)行加1,然后再與7比較,如果等于7的話,就設(shè)置為1,因此可以看出diceRoll的變化是1,2,3,4,5,6,1……
在擲骰子之后,玩家移動(dòng)diceRoll指示的步數(shù),這時(shí)可能已經(jīng)超過了finalSquare,因此需要進(jìn)行if判斷,如果為true的話,執(zhí)行該格子上的事件:如果是普通格子就不動(dòng),如果是梯子或者蛇就移動(dòng)相應(yīng)的步數(shù),這里只需要直接使用square += board[square]就可以了。
在while循環(huán)執(zhí)行完畢之后,重新檢查條件square < finalSquare是否成立,繼續(xù)游戲直到游戲結(jié)束。
Do-while循環(huán)
另一種while循環(huán)是do-while循環(huán)。在這種循環(huán)中,循環(huán)體中的語句會(huì)先被執(zhí)行一次,然后才開始檢測(cè)循環(huán)條件是否滿足,下面是do-while循環(huán)的一般形式:
do {
statements
} while condition
上面的蛇與梯子的游戲使用do-while循環(huán)來寫可以這樣完成。初始化語句和while循環(huán)的類似:
let finalSquare = 25
var board = Int[](count: finalSquare + 1, repeatedValue: 0)
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
var square = 0
var diceRoll = 0
在這種循環(huán)中,第一個(gè)動(dòng)作就是檢測(cè)是否落在梯子或者蛇上,因?yàn)闆]有梯子或者蛇可以讓玩家直接到達(dá)第25格,所以游戲不會(huì)直接結(jié)束,接下來的過程就和上面的while循環(huán)類似了,循環(huán)的條件語句還是檢測(cè)是否已經(jīng)達(dá)到最終格子。
do {
// move up or down for a snake or ladder
square += board[square]
// roll the dice
if ++diceRoll == 7 { diceRoll = 1 }
// move by the rolled amount
square += diceRoll
} while square < finalSquare
println("Game over!")
3、條件語句
通常情況下我們都需要根據(jù)不同條件來執(zhí)行不同語句。比如當(dāng)錯(cuò)誤發(fā)生的時(shí)候,執(zhí)行一些錯(cuò)誤信息的語句,告訴編程人員這個(gè)值是太大了還是太小了等等。這里就需要用到條件語句。
Swift提供了兩種條件分支語句的方式,if語句和switch語句。一般if語句比較常用,但是只能檢測(cè)少量的條件情況。switch語句用于大量的條件可能發(fā)生時(shí)的條件語句。
if語句
在最基本的if語句中,條件語句只有一個(gè),如果條件為true時(shí),執(zhí)行if語句塊中的語句:
var temperatureInFahrenheit = 30
if temperatureInFahrenheit <= 32 {
println("It's very cold. Consider wearing a scarf.")
}
// prints "It's very cold. Consider wearing a scarf."
上面這個(gè)例子檢測(cè)溫度是不是比32華氏度(32華氏度是水的冰點(diǎn),和攝氏度不一樣)低,如果低的話就會(huì)輸出一行語句。如果不低,則不會(huì)輸出。if語句塊是用大括號(hào)包含的部分。
當(dāng)條件語句有多種可能時(shí),就會(huì)用到else語句,當(dāng)if為false時(shí),else語句開始執(zhí)行:
temperatureInFahrenheit = 40
if temperatureInFahrenheit <= 32 {
println("It's very cold. Consider wearing a scarf.")
} else {
println("It's not that cold. Wear a t-shirt.")
}
// prints "It's not that cold. Wear a t-shirt."
在這種情況下,兩個(gè)分支的其中一個(gè)一定會(huì)被執(zhí)行。
同樣也可以有多個(gè)分支,使用多次if和else
temperatureInFahrenheit = 90
if temperatureInFahrenheit <= 32 {
println("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
println("It's really warm. Don't forget to wear sunscreen.")
} else {
println("It's not that cold. Wear a t-shirt.")
}
// prints "It's really warm. Don't forget to wear sunscreen."
上面這個(gè)例子中有多個(gè)if出現(xiàn),用來判斷溫度是太低還是太高,最后一個(gè)else表示的是溫度不高不低的時(shí)候。
當(dāng)然else也可以被省掉
temperatureInFahrenheit = 72
if temperatureInFahrenheit <= 32 {
println("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
println("It's really warm. Don't forget to wear sunscreen.")
}
在這個(gè)例子中,溫度不高不低的時(shí)候不會(huì)輸入任何信息。
switch語句
switch語句考察一個(gè)值的多種可能性,將它與多個(gè)case相比較,從而決定執(zhí)行哪一個(gè)分支的代碼。switch語句和if語句不同的是,它還可以提供多種情況同時(shí)匹配時(shí),執(zhí)行多個(gè)語句塊。
switch語句的一般結(jié)構(gòu)是:
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
}
每個(gè)switch語句包含有多個(gè)case語句塊,除了直接比較值以外,Swift還提供了多種更加復(fù)雜的模式匹配的方式來選擇語句執(zhí)行的分支,這在后續(xù)的小節(jié)會(huì)繼續(xù)介紹。
在switch中,每一個(gè)case分支都會(huì)被匹配和檢測(cè)到,所有case沒有提到的情況都必須使用default關(guān)鍵詞。注意default關(guān)鍵詞必須在所有case的最后。
下面的例子用switch語句來判斷一個(gè)字符的類型:
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")
}
// prints "e is a vowel"
在這個(gè)例子中,首先看這個(gè)字符是不是元音字母,再檢測(cè)是不是輔音字母。其它的情況都用default來匹配即可。
不會(huì)一直執(zhí)行
跟C和Objective-C不同,Swift中的switch語句不會(huì)因?yàn)樵赾ase語句的結(jié)尾沒有break就跳轉(zhuǎn)到下一個(gè)case語句執(zhí)行。switch語句只會(huì)執(zhí)行匹配上的case里的語句,然后就會(huì)直接停止。這樣可以讓switch語句更加安全,因?yàn)楹芏鄷r(shí)候編程人員都會(huì)忘記寫break。
每一個(gè)case中都需要有可以執(zhí)行的語句,下面的例子就是不正確的:
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a":
case "A":
println("The letter A")
default:
println("Not the letter A")
}
// this will report a compile-time error
跟C不同,switch語句不會(huì)同時(shí)匹配a和A,它會(huì)直接報(bào)錯(cuò)。一個(gè)case中可以有多個(gè)條件,用逗號(hào),分隔即可:
switch some value to consider {
case value 1,
value 2:
statements
}
范圍匹配
switch語句的case中可以匹配一個(gè)數(shù)值范圍,比如:
let count = 3_000_000_000_000
let countedThings = "stars in the Milky Way"
var naturalCount: String
switch count {
case 0:
naturalCount = "no"
case 1...3:
naturalCount = "a few"
case 4...9:
naturalCount = "several"
case 10...99:
naturalCount = "tens of"
case 100...999:
naturalCount = "hundreds of"
case 1000...999_999:
naturalCount = "thousands of"
default:
naturalCount = "millions and millions of"
}
println("There are /(naturalCount) /(countedThings).")
// prints "There are millions and millions of stars in the Milky Way."
元組
case中還可以直接測(cè)試元組是否符合相應(yīng)的條件,_可以匹配任意值。
下面的例子是判斷(x,y)是否在矩形中,元組類型是(Int,Int)
let somePoint = (1, 1)
switch somePoint {
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")
}
// prints "(1, 1) is inside the box"
和C語言不同,Swift可以判斷元組是否符合條件。
數(shù)值綁定
在case匹配的同時(shí),可以將switch語句中的值綁定給一個(gè)特定的常量或者變量,以便在case的語句中使用。比如:
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"
switch語句判斷一個(gè)點(diǎn)是在x軸上還是y軸上,或者在其他地方。這里用到了匹配和數(shù)值綁定。第一種情況,如果點(diǎn)是(x,0)模式的,將值綁定到x上,這樣在case語句中可以輸出該值。同理如果在y軸上,就輸出y的值。
Where關(guān)鍵詞
switch語句可以使用where關(guān)鍵詞來增加判斷的條件,在下面的例子中:
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"
每個(gè)case都因?yàn)橛衱here而不同,第一個(gè)case就是判斷x是否與y相等,表示點(diǎn)在斜線y=x上。
4、控制跳轉(zhuǎn)語句
在Swift中控制跳轉(zhuǎn)語句有4種,讓編程人員更好地控制代碼的流轉(zhuǎn),包括:
continue
break
fallthrough
return
其中continue,break和fallthrough在下面詳細(xì)介紹,return語句將在函數(shù)一章介紹。
continue
continue語句告訴一個(gè)循環(huán)停止現(xiàn)在在執(zhí)行的語句,開始下一次循環(huán)。
注意:在for-condition-increment循環(huán)中,increment增量語句依然執(zhí)行,只是略過了一次循環(huán)體。
下面的例子實(shí)現(xiàn)的是去除一個(gè)字符串中的空格和元音字母,從而組成一個(gè)字謎:
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput {
switch character {
case "a", "e", "i", "o", "u", " ":
continue
default:
puzzleOutput += character
}
}
println(puzzleOutput)
// prints "grtmndsthnklk"
遍歷字符串的每一個(gè)字符,當(dāng)遇到元音字母或者空格時(shí)就忽略,進(jìn)行下一次循環(huán),從而得到了最終的字謎。
break
break語句將終止整個(gè)循環(huán)的執(zhí)行,可以用在循環(huán)語句中,也可以用在switch語句中。
let numberSymbol: Character = "三" // Simplified Chinese for the number 3
var possibleIntegerValue: Int?
switch numberSymbol {
case "1", "?", "一", "?":
possibleIntegerValue = 1
case "2", "?", "二", "?":
possibleIntegerValue = 2
case "3", "?", "三", "?":
possibleIntegerValue = 3
case "4", "?", "四", "?":
possibleIntegerValue = 4
default:
break
}
if let integerValue = possibleIntegerValue {
println("The integer value of /(numberSymbol) is /(integerValue).")
} else {
println("An integer value could not be found for /(numberSymbol).")
}
// prints "The integer value of 三 is 3."
上面的例子首先檢查numberSymbol是不是一個(gè)數(shù)字,阿拉伯?dāng)?shù)字,漢字,拉丁文或者泰文都可以。如果匹配完成,則將possibleIntegerValue賦值。最后在通過if語句檢測(cè)是否已被賦值,并綁定到integerValue常量上,最后輸出。default語句用來迎接未能被上述case匹配的情況,但是不需要做任何事情,因此直接使用break終止即可。
fallthrough
由于Swift中的switch語句不會(huì)自動(dòng)的因?yàn)闆]有break而跳轉(zhuǎn)到下一個(gè)case,因此如果需要想C語言中那樣,依次執(zhí)行每個(gè)case的時(shí)候,就需要用到fallthrough關(guān)鍵詞。
像下面這個(gè)例子一樣,default分支最終都會(huì)被執(zhí)行:
let integerToDescribe = 5
var description = "The number /(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and also"
fallthrough
default:
description += " an integer."
}
println(description)
// prints "The number 5 is a prime number, and also an integer."
標(biāo)簽語句
switch和循環(huán)可以互相嵌套,循環(huán)之間也可以互相嵌套,因此在使用break或者continue的時(shí)候,需要知道到底是對(duì)哪個(gè)語句起作用。這就需要用到標(biāo)簽語句。標(biāo)簽語句的一般形式如下:
label name: while condition {
statements
}
下面的例子演示了如何使用標(biāo)簽語句以及嵌套的循環(huán)和switch。
依然采用之前的那個(gè)梯子與蛇的游戲,第一步依然是設(shè)置初始值:
let finalSquare = 25
var board = Int[](count: finalSquare + 1, repeatedValue: 0)
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
var square = 0
var diceRoll = 0
然后,使用一個(gè)while循環(huán)與switch的嵌套來完成游戲
gameLoop: while square != finalSquare {
if ++diceRoll == 7 { diceRoll = 1 }
switch square + diceRoll {
case finalSquare:
// diceRoll will move us to the final square, so the game is over
break gameLoop
case let newSquare where newSquare > finalSquare:
// diceRoll will move us beyond the final square, so roll again
continue gameLoop
default:
// this is a valid move, so find out its effect
square += diceRoll
square += board[square]
}
}
println("Game over!")
在這個(gè)代碼中,將游戲的循環(huán)命名為gameLoop,然后在每一步移動(dòng)格子時(shí),判斷當(dāng)前是否到達(dá)了游戲終點(diǎn),在break的時(shí)候,需要將整個(gè)游戲循環(huán)終止掉,而不是終止switch,因此用到了break gameLoop。同樣的,在第二個(gè)分支中,continue gameLoop也指明了需要continue的是整個(gè)游戲,而不是switch語句本身。