循环语句
for…in 循环
// 遍历数组 (Array)
let names = ["zero", "hour", "ray", "lee"]
for name in names {
print("Hello, \(name)!")
}
// 遍历字典 (Dictionary)
let animalLegs = ["spider":8, "ant":6, "cat":4]
for (name, legCount) in animalLegs {
print("\(name) has \(legCount) legs")
}
// 遍历区间 (Range)
let range = 1...5
for i in range {
print("\(i) times 5 is \(i * 5)")
}// 忽略区间内每一项的值 _
var answer = 1
for _ in range {
answer *= 3
}
print("3 to the power of 5 is \(answer)")
while循环
while currentWrongCount < 5 {
print("请输入密码")
let input = readLine()
if input == "123456" {
print("密码正确")
currentWrongCount = 5
} else {
print("密码错误")
currentWrongCount += 1
}
}
repeat…while 循环
repeat {
print("请输入密码")
let input = readLine()
if input == "123456" {
print("密码正确")
currentWrongCount = 5
} else {
print("密码错误")
currentWrongCount += 1
}
} while currentWrongCount < 5
条件语句
if 语句
// if 语句
let isGirl = true
if isGirl {
print("我们出去玩会吧!")
}
// if...else
if isGirl {
print("我们去电影院看电影啊!")
} else {
print("我们用电脑看电影吧!")
}
// if...else if...else 最后一个else可以省略
let score = 40
if score >= 90 {
print("你成绩好好哟!")
} else if score >= 80 {
print("成绩不错呀, 咋学的?")
} else if score >= 70 {
print("可以啊兄弟, 加把劲儿!")
} else {
print("猪!")
}
switch…case语句
let someChar: Character = "b"
switch someChar {
case "a":
print("first character")
case "z":
print("last character")
default:
print("other character")
}
同时匹配多个值
switch someChar {
case "a", "A":
print("letter a or A")
default:
print("not the letter a or A")
}
区间匹配
let count = 62
let naturalCount: String
switch count {
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(naturalCount)
元组的匹配
let point = (1, 1)
switch point {
case (0, 0):
print("\(point) is at the origin")
case (_, 0):
print("\(point) is at the x-axis")
case (0, _):
print("\(point) is at the y-axis")
case (-2...2, -2...2):
print("\(point) is inside the box")
default:
print("\(point) is outside the box")
}
值绑定 Value Bindings
// case分支允许将匹配的值声明为常量或变量, 并且在分支体内使用 - 这种行为称为值绑定
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
print("在x轴上\(x)处")
case (0, let y):
print("在y轴上\(y)处")
case let (x, y):
print("在(\(x), \(y))处")
}
where的使用
let anyPoint = (1, -1)
switch anyPoint {
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 a common point)")
}
复合型cases
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 consonant")
}
复合 + 值绑定
let stillPoint = (3, 0)
switch stillPoint {
case (let distance, 0), (0, let distance):
print("在坐标轴上, 距离原点\(distance)")
default:
print("不在坐标轴上")
}
控制转移语句
continue语句
// 停止本次循环, 开始下次循环
let hiStr = "hello, swift"
var output = ""
for character in hiStr {
switch character {
case "a", "e", "i", "o", "u":
continue
default:
output.append(character)
}
}
print(output)
break 语句
// 立即结束整个循环
for index in 1...5 {
if index == 3 {
break
} else {
print(index)
}
}
fallthrough 语句
// 从上一个case分支中跳转到下一个case分支中
let integerValue = 5
var desc = "The number \(integerValue) is"
switch integerValue {
case 2, 3, 5, 7, 11, 13, 17, 19:
desc += " a prime number, and also"
fallthrough
default:
desc += " an integer."
}
print(desc)
带标签的语句 statement label
loop: for i in 1...3 {
print("第\(i)次循环")
switch i {
case 1:
continue
case 2:
break loop // 在这里如果想要跳出for循环, 就要用到带标签的循环语句
default:
print("default")
}
}
guard语句
// 条件为真时, 执行guard语句后的代码, 否则执行else语句
func greet(person: [String: String]) {
guard let name = person["name"] else {
return
}
print("Hello, \(name)!")
guard let location = person["location"] else {
print("I hope the weather is nice near you")
return
}
print("I hope the weather is nice in \(location)")
}
greet(person: ["name":"Ray", "location":"BJ"])