For-In
for index in 1...5 {
//閉區間操作符(...)
print("\(index) times 5 is \(index * 5)")
//注意index 常量隻存在于循環的生命周期裡。
}
/*
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
*/
如果不需要知道範圍内的每一項的值,可以使用下劃線(_)替代變量名來忽略對值的通路
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base // 3的十次幂
}
print ("\(base) to the power of \(power) is\(answer)")
//3 to the power of 10 is59049
使用for-in周遊一個數組所有元素
let names = ["Anna","Alex","Brian","Jack"]
for name in names {
print("Hello,\(name)!")
}
let numberOfLegs = ["spider":8,"ant":6,"cat":4]
for (animalName,legCount) in numberOfLegs {
print("\(animalName)s have \(legCount) legs")
}
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
print("\(animalName)s have \(legCount) legs")
}
// ants have 6 legs
// cats have 4 legs
// spiders have 8 legs
For條件遞增 (for-condition-increment)
for var index = 0; index < 3; ++index {
print("index is \(index)")
}
var index:Int
for index = 0; index < 3; ++index {
print("index is \(index)")
}
print("The loop statements were executed \(index) times")
while 循環
while condition {
statements
}
let finalSqure = 25
var board = [Int](count: finalSqure + 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
while square < finalSqure {
if ++diceRoll == 7 { diceRoll = 1 }
square += diceRoll
if square < board.count {
square += board[square]
}
}
print("GameOver!")
Do-While
do {
statements
} while condition
//條件語句
//If
var temperatureInFahrenheit = 30
if temperatureInFahrenheit <= 32 {
print("it's very cold. Consider wearing a scarf")
}
// Switch
let somCharacter:Character = "e"
switch somCharacter {
case "a","b":
print("1")
case "c","d","e":
print("2")
default:
print("nothing")
}
元祖,在同一個switch語句中測試多個值。元組中的元素可以是值,也可以是範圍。使用(_)來比對所有可能的值
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")
// (1,1) is inside the box
}
值綁定
case塊的模式允許将比對的值綁定到一個臨時的常量或者變量,這些常量或變量在該case塊裡就可以被引用了
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("somewhee else at (\(x),\(y))")
// on the x-axis with an x value of 2
}
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 linx x == -y)")
case let (x,y):
print("(\(x),\(y) is just some arbirthary point)")
// (1,-1 is on the linx x == -y)
}
Swift四種控制轉移語句
-continue
-break
-fallthrough
-return
//continue 語句告訴循環停止正在做的事情并且再次從開始循環的下一次周遊。就是說“我不再繼續目前的循環周遊了”而不是離開整個的循環
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput.characters {
switch character {
case "a" ,"e","i","o","u":
continue
default:
puzzleOutput.append(character)
}
}
print(puzzleOutput) //grt mnds thnk lk
Break 立即結束整個控制流語句。
當在switch語句裡使用時,break 導緻 switch 語句立即結束它的執行,并且轉移控制到 switch 語句結束花括号( })之後的第一行代碼上
let numberSymbol: Character = "三"
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 {
print("The integer value of \(numberSymbol) is \(integerValue).")
} else {
print("An integer value could not be found for \(numberSymbol).")
}
//The integer value of 三 is 3.
Fallthrough
如果你确實需要 C 風格的貫穿行為,你可以選擇在每個情況末尾使用 fallthrough 關鍵字。
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
// 使用 fallthrough 關鍵字來“貫穿到” default 情況。 default 情況添加額外的文字到描述的末尾,接着 switch 語句結束。
default:
description += "an integer."
}
print(description)