for in
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
}
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
print("\(animalName)s have \(legCount) legs")
}
While 循環(huán)
//while 循環(huán)通過判斷單一的條件開始叹话。如果條件為 true ,語句的合集就會重復(fù)執(zhí)行直到條件變?yōu)?false 肛根。
while condition {
statements
}
repeat-while 循環(huán)(do while)
repeat {
statements
} while condition
repeat {
// move up or down for a snake or ladder
square += board[square]
// roll the dice
diceRoll += 1
if diceRoll == 7 { diceRoll = 1 }
// move by the rolled amount
square += diceRoll
} while square < finalSquare
print("Game over!")
Switch(相比 C 和 Objective-C 里的 switch 語句來說,Swift 里的 switch 語句不會默認(rèn)從每個情況的末尾貫穿到下一個情況里。相反,整個 switch 語句會在匹配到第一個 switch 情況執(zhí)行完畢之后退出框全,不再需要顯式的 break 語句。這使得 switch 語句比 C 的更安全和易用干签,并且避免了意外地執(zhí)行多個 switch 情況津辩。)
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a", "A":
print("The letter A")
default:
print("Not the letter A")
}
// Prints "The letter A"
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).")
// prints "There are dozens of moons orbiting Saturn."
元組 (元組竟然是控制流~~)
//你可以使用元組來在一個 switch 語句中測試多個值。每個元組中的元素都可以與不同的值或者區(qū)間進(jìn)行匹配。另外喘沿,使用下劃線( _)來表明匹配所有可能的值闸度。
//下邊的例子接收一個 (x,y) 點坐標(biāo),用一個簡單的元組類型 (Int,Int) 蚜印,并且在后邊顯示在圖片中:
let somePoint = (1, 1)
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")
}
//元組 值綁定:switch 情況可以將匹配到的值臨時綁定為一個常量或者變量莺禁,來給情況的函數(shù)體使用。這就是所謂的值綁定窄赋,因為值是在情況的函數(shù)體里“綁定”到臨時的常量或者變量的哟冬。
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
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 情況可以使用 where 分句來檢查額外的情況。
下邊的栗子劃分 (x,y) 坐標(biāo)到下邊的圖例中:
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):
print("(\(x), \(y)) is just some arbitrary point")
}
//復(fù)合情況:多個 switch 共享同一個函數(shù)體的多個情況可以在 case 后寫多個模式來復(fù)合忆绰,在每個模式之間用逗號分隔浩峡。如果任何一個模式匹配了,那么這個情況都會被認(rèn)為是匹配的错敢。如果模式太長翰灾,可以把它們寫成多行,比如說:
let someCharacter: Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
print("\(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":
print("\(someCharacter) is a consonant")
default:
print("\(someCharacter) is not a vowel or a consonant")
}
// Prints "e is a vowel"
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")
}
// Prints "On an axis, 9 from the origin"
//控制轉(zhuǎn)移語句
//continue: 跳過
//break:跳出
//fallthrough:貫穿
//return:結(jié)束
//throw:拋出異常