1.if-else
let age = 4
if age >= 22 {
print("Get married")
} else if age >= 18 {
print("Being a adult")
} else if age >= 7 {
print("Go to school")
} else {
print("Just a child")
}
if后面的条件可以省略小括号
条件后面的大括号不可以省略
if后面的条件只能是Bool类型
2.while
var num = 5
while num > 0 {
print("num is \(num)")
num -= 1 } // 打印了5次
repeat-while
repeat-while相当于C语言中的do-while
var num = 1
repeat {
print("num is \(num)")
} while num > 0 // 打印了1次
for
2.1.闭区间运算符:a…b, a <= 取值 <= b
let names = ["Anna", "Alex", "Brian", "Jack"]
for i in 0...3 {
print(names[i])
} // Anna Alex Brian Jack
保存区间范围
let range = 1...3
for i in range {
print(names[i])
} // Alex Brian Jack
变量作为区间
let a = 1
var b = 2
for i in a...b {
print(names[i])
} // Alex Brian
i默认是let,i需要改变时可以声明为var
for var i in 1...3 {
i += 5
print(i)
} // 6 7 8
不需要i时省略
for _ in 1...3 {
print("for")
} // 打印了3次
2.2.半开区间运算符:a…<b, a <= 取值 < b
for i in 1..<5 {
print(i)
} // 1 2 3 4
2.3.单侧区间:让区间朝一个方向尽可能的远
for name in names[2...] {
print(name)
} // Brian Jack
2.4.for – 区间运算符用在数组上
for name in names[0...3] {
print(name)
} // Anna Alex Brian Jack
2.5.区间是否包含
let range = ...5
range.contains(7) // false
range.contains(4) // true
range.contains(-3) // true
2.6.带间隔的区间值
let hours = 11
let hourInterval = 2
// tickMark的取值:从4开始,累加2,不超过11
for tickMark in stride(from: 4, through: hours, by: hourInterval) {
print(tickMark)
} // 4 6 8 10
3.switch
var number = 1
switch number {
case 1:
print("number is 1")
case 2:
print("number is 2")
default:
print("number is other")
} // number is 1
默认可以不写break,并不会贯穿到后面的条件
case、default后面不能写大括号{}
3.2fallthrough
var number = 1
switch number {
case 1:
print("number is 1")
fallthrough
case 2:
print("number is 2")
default:
print("number is other")
}
// number is 1
// number is 2
3.3switch注意点
switch必须要保证能处理所有情况
如果能保证已处理所有情况,也可以不必使用default
case、default后面至少要有一条语句
如果不想做任何事,加个break即可
switch也支持Character、String类型
3.4区间匹配、元组匹配
区间匹配
let count = 62
switch count {
case 0:
print("none")
case 1..<5:
print("a few")
case 5..<12:
print("several")
case 12..<100:
print("dozens of")
case 100..<1000:
print("hundreds of")
default:
print("many")
} // dozens of
元组匹配
let point = (1, 1)
switch point {
case (0, 0):
print("the origin")
case (_, 0):
print("on the x-axis")
case (0, _):
print("on the y-axis")
case (-2...2, -2...2):
print("inside the box")
default:
print("outside of the box") } // inside the box
值绑定(模式匹配(x, y))
对于元组来说,只要符合那么就会赋值到case的元组中的变量中(变量可以符合任意的值)。
必要时let也可以改为var
let point = (2, 0)
switch point {
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))")
} // on the x-axis with an x value of 2
4.where(条件过滤)
满足条件才会执行。
let point = (1, -1)
switch point {
case let (x, y) where x == y:
print("on the line x == y")
case let (x, y) where x == -y:
print("on the line x == -y")
case let (x, y):
print("(\(x), \(y)) is just some arbitrary point")
} // on the line x == -y
// 将所有正数加起来
var numbers = [10, 20, -10, -20, 30, -30]
var sum = 0
for num in numbers where num > 0 { // 使用where来过滤num
sum += num
}
print(sum) // 60
5.标签语句
在指定的标签位置继续执行或者结束执行
outer: for i in 1...4 {
for k in 1...4 {
if k == 3 {
continue outer
}
if i == 3 {
break outer
}
print("i == \(i), k == \(k)")
}
}