Swift 3.0學(xué)習(xí)筆記_8_控制流

包含 for-in, while, repeat-while, if, switch 的知識點(diǎn)

//控制流
//1.for-in 可遍歷一個集合中的所有元素,例如數(shù)字范圍,數(shù)組中的元素或者字符串中的字符.
for index in 1...5 { //...是閉區(qū)間, ..<表示半閉區(qū)間
    print("\(index)")
}
//index 是一個每次循環(huán)遍歷開始時被自動賦值的常量,這種情況下, index 在使用前不需要聲明,只需要將它包含在循環(huán)的聲明中,就可以對其進(jìn)行隱式聲明,而無需使用 let 關(guān)鍵字聲明.
//若不需要區(qū)間序列內(nèi)每一項(xiàng)的值,你可以使用下劃線(_)替代變量名來忽略這個值:
let base   = 3
let power  = 10
var answer = 1
for _ in 1...power { //一般用于只需獲取 循環(huán)次數(shù) 而無需關(guān)心每次循環(huán)時的值的情況
    answer *= base
}
print("\(base) to the power of (power) is \(answer)") //計(jì)算 3的 10次冪

//使用 for- in 遍歷數(shù)組元素
let names = ["Anna","Alex","Brain","Jack"]
for name in names {
    print("\(name)")
}
//使用 for-in 遍歷字典
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName,legCount) in numberOfLegs {
    print("\(animalName)s have \(legCount) legs")
}

//2.while 循環(huán)會一直運(yùn)行一段語句直到條件變?yōu)?false. 該類循環(huán)適合使用在迭代次數(shù)未知的情況下.
//while 循環(huán)有兩種形式:
//1> while循環(huán),每次在循環(huán)開始時計(jì)算條件是否符合;
//2> repeat-while 循環(huán),每次在循環(huán)結(jié)束時計(jì)算條件是否符合.

//2.1. while 循環(huán)
//while 循環(huán)從計(jì)算一個條件開始,如果條件為 true, 會重復(fù)運(yùn)行一段語句,直到條件變?yōu)?false
/**
 while condition {
    statements
 }
 */

//2.2. repeat-while 循環(huán)
//repeat-while 循環(huán)與 while 的區(qū)別是在判斷執(zhí)行條件的時間上,repeat-while 是先執(zhí)行一次循環(huán)然后判斷條件,直至條件變?yōu)?false
//repeat-while 循環(huán)類似于 do-while 循環(huán),即最少執(zhí)行一次循環(huán)體
/**
 repeat {
    statements
 } while condition
 */

//3.條件語句
//swift 提供兩種條件語句,即 if語句 和 switch語句,通常,當(dāng)條件較為簡單且可能的情況較少時使用 if語句,否則應(yīng)使用 switch語句.

//3.1. if 語句
//3.2. switch 語句
//switch 語句會嘗試把某個值與若干個模式進(jìn)行匹配,根據(jù)第一個匹配成功的模式, switch 語句會執(zhí)行對應(yīng)的代碼.
/**
 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
 }
 */

//注: switch 語句必須是完備的,也就是說,每一個可能的值都必須至少有一個 case 分支與之對應(yīng),在某些不可能涵蓋所有值的情況下,可以使用 default 分支來覆蓋其它沒有對應(yīng)的值,這個默認(rèn)分支必須在 switch 語句的最后面.
let someCharacter:Character = "z"
switch someCharacter {
case "a":
    print("The first letter of the alphabet")
case "z":
    print("The last letter of the alphabet")
default:
    print("Some other character")
}

//注:每一個 case 分支語句都必須要包含最少一條語句,下面的寫法是不對的:
/**
 switch test {
 case 1: //這里是錯誤的,應(yīng)包含最少一條語句
 case 2:
    print("something")
 }
 */

//單個 case 可以進(jìn)行復(fù)合匹配
let anotherCharacter:Character = "a"
switch anotherCharacter {
case "a","A": //逗號分隔
    print("the letter A")
default:
    print("Not the letter A")
}

//case分支的模式也可以是一個值的區(qū)間
let approximateCount = 62
let countedThings = "moons orbiting Saturn"
var naturalCount: String
switch approximateCount {
case 0:
    naturalCount = "no"
case 1..<5:
    naturalCount = "a few"
case 5..<12:
    naturalCount = "several"
case 12..<100:
    naturalCount = "dozens of"
case 100..<1000:
    naturalCount = "hundreds of"
default:
    naturalCount = "many"
}
print("There are \(naturalCount) \(countedThings).")

//switch 與 元組
let somePoint = (1, 1) //(Int,Int)類型的元組
switch somePoint {
case (0, 0):
    print("(0, 0) is at the origin")
case (_, 0):
    print("(\(somePoint.0), 0) is on the x-axis")
case (0, _):
    print("(0, \(somePoint.1)) is on the y-axis")
case (-2...2, -2...2):
    print("(\(somePoint.0), \(somePoint.1)) is inside the box")
default:
    print("(\(somePoint.0), \(somePoint.1)) is outside of the box")
}

//注:swift 允許多個 case 匹配同一個值,如果存在多個匹配,那么只會執(zhí)行第一個被匹配到的 case 分支,其他的匹配項(xiàng)會被忽略掉.

//值綁定: case分支允許將匹配的值綁定到一個臨時的常量或變量,并且在 case 分支體內(nèi)使用.這種行為被稱為值綁定.
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0): // x 為臨時變量
    print("on the x-axis with an x value of \(x)")
case (0, let y):
    print("on the y-axis with a y value of \(y)")
case let (x, y):
    print("somewhere else at (\(x), \(y))")
}
//注:上面的 switch 未包含默認(rèn)分支,這是因?yàn)樽詈笠粋€ case let((x,y)聲明了一個可以匹配余下所有值得元組,這使得 switch 語句已經(jīng)完備,因此不需要再書寫默認(rèn)分支.

// where: case 分支的模式可以使用 where 語句來判斷額外的條件
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
    print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
    print("(\(x), \(y)) is on the line x == -y")
case let (x, y): //該語句的存在使得 switch 無需再寫默認(rèn)分支語句
    print("(\(x), \(y)) is just some arbitrary point")
}

//復(fù)合匹配:當(dāng)多個條件可以使用同一種方法來處理時,可以將這幾種可能放在同一個 case 后面,并且用逗號隔開,當(dāng) case 后面的任意一種模式匹配的時候,這條分支就會被匹配.并且,如果匹配列表過長,還可以分行書寫.
let anotherSomeCharacter: Character = "e"
switch anotherSomeCharacter {
case "a", "e", "i", "o", "u":
    print("\(anotherSomeCharacter) 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":
    print("\(anotherSomeCharacter) is a consonant")
default:
    print("\(anotherSomeCharacter) is not a vowel or a consonant")
}

//符合匹配內(nèi)也可以包含值綁定:復(fù)合匹配里所有的匹配模式,都必須包含相同的值綁定。并且每一個綁定都必須獲取到相同類型的值。這保證了無論復(fù)合匹配中的哪個模式發(fā)生了匹配,分支體內(nèi)的代碼都能獲取到綁定的值,并且綁定的值都有一樣的類型。
let stillAnotherPoint = (9, 0)
switch stillAnotherPoint {
case (let distance, 0), (0, let distance):
    print("On an axis, \(distance) from the origin")
default:
    print("Not on an axis")
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容