1: For-in 循环
let arr = ["张三","李四","王二"]
fornameinarr {
print(name)
}
//张三
//李四
//王二
2: While 循环
while循环通过判断单一的条件开始。如果条件为true,语句的合集就会重复执行直到条件变为false。(条件符合接着遍历, 条件不符跳出循环)
var num:Int=5
while num > 0 {
num -= 1
}
print(num) // 0
3:If 语句
let name = "haha"
if name == "haha" {
print("true")
} else {
print("false")
}
// true
4: Switch 语句
let num = 5
var str:String
switch num {
case -1...0:
str ="bad1"
case1...4:
str ="bad2"
case5..<10:
str ="good"
default:
str ="bad3"
}
print(str) // "good"
5: guard -- else 语句
guard语句,类似于if语句,基于布尔值表达式来执行语句. guard语句总是有一个else分句——else分句里的代码会在条件不为真的时候执行。
let person = ["name":"gouzi"]
guard let name = person["name"] else {
return
}
print("Hello\(name) !") // "Hello gouzi !"