if else

if 后面条件可以省略小括号
条件后面打括号不可以省略

while

  1. var num = 5
  2. while num > 0 {
  3. print("num is \(num)")
  4. num -= 1
  5. }
  6. var num = -1
  7. repeat {
  8. print("num is \(num)")
  9. } while num > 0

repeat-while 相当于C语言中的do-while
这里不是num—,是因为Swift3开始,去除了自增(++)、自减(—)运算符
(阅读性较差)

for

闭区间运算符: a…b,a <= 取值 <= b

  1. let names = ["a", "b", "c", "d"]
  2. for i in 0...3 {
  3. print(names[i])
  4. }

或者 let range = 0…3, for i in range这样遍历
如果没有用到i,可以用下划线代替

  1. for _ in 0...3 {
  2. }

半开区间运算符:a..区间运算符用在数组上

  1. for name in names[0...3] {
  2. print(name)
  3. }

单侧区间:让区间朝一个方向尽可能的远

  1. for name in names[...3] {
  2. print(name)
  3. }
  4. for name in names[..<3] {
  5. print(name)
  6. }

也可以将区间赋值给range

  1. // 负无穷到5
  2. let range = ...5
  3. range.contains(7) // true
  4. range.contains(3) // false

字符、字符串也能使用区间运算符,但默认不能在for-in中

  1. let range = "a"..."z"
  2. range.contains("c")
  3. range.contains("e")

带间隔的区间值

  1. /// 打印0-100间的偶数
  2. for i in stride(from: 0, through: 100, by: 2) {
  3. print(i)
  4. }

switch

  1. let num = 1
  2. switch num {
  3. case 1:
  4. print("num is 1")
  5. case 2:
  6. print("num is 2")
  7. case 3:
  8. print("num is 3")
  9. default:
  10. print("num is other")
  11. }
  12. // num is 1

case、default后面不能写大括号{}
默认可以不写break,并不会贯穿到后面的条件
fallthrough,可以实现贯穿效果

  1. let num = 1
  2. switch num {
  3. case 1:
  4. print("num is 1")
  5. fallthrough
  6. case 2:
  7. print("num is 2")
  8. case 3:
  9. print("num is 3")
  10. default:
  11. print("num is other")
  12. }
  13. // num is 1
  14. // num is 2

switch必须保证能处理所有情况,下面这种情况不写default就会报错
image.png
case、default后面至少有一条语句,如果不想做任何事情,加个break即可
image.png
如果能保证处理所有情况,也不必使用default,例如枚举

  1. enum Answer {
  2. case right
  3. case wrong
  4. }
  5. let answer = Answer.right
  6. switch answer {
  7. case .right:
  8. print("answer is right ")
  9. case .wrong:
  10. print("answer is right ")
  11. }

复合条件
switch也支持Character、String类型

  1. let string = "Jack"
  2. switch string {
  3. case "Jack":
  4. print("is jack")
  5. case "Tom":
  6. print("is Tom")
  7. default:
  8. break
  9. }

复合条件可以使用逗号分隔条件,也可以用fallthrough关键字

  1. switch name {
  2. case "Jack", "Rose":
  3. print("is Jack and Rose")
  4. case "Tom":
  5. print("is Tom")
  6. default:
  7. break
  8. }
  9. switch name {
  10. case "Jack":
  11. print("is jack")
  12. fallthrough
  13. case "Rose":
  14. print("is Rose")
  15. case "Tom":
  16. print("is Tom")
  17. default:
  18. break
  19. }

字符类型也一样:

  1. let c: Character = "a"
  2. switch c {
  3. case "a":
  4. print("is a")
  5. case "A":
  6. print("is A")
  7. default
  8. break
  9. }

区间匹配、元组匹配

  1. let count = 62
  2. switch count {
  3. case 1..<5:
  4. print("1..<5")
  5. case 6..<100:
  6. print("6..<100")
  7. default:
  8. break
  9. }
  1. let point = (1, 1)
  2. switch point {
  3. case (0, 0):
  4. print("is zero point")
  5. case (_, 0):
  6. print("is on x-axis")
  7. case (0, _):
  8. print("is on y-axis")
  9. default:
  10. break
  11. }

可以使用下划线忽略某个值,case匹配问题,属于模式匹配(Pattern Matching)的范畴
值绑定

  1. switch point {
  2. case (let x, 0):
  3. print("x is \(x)")
  4. case (0, let y):
  5. print("y is \(y)")
  6. case let (x, y):
  7. print("x is \(x), y is \(y)")
  8. default:
  9. break
  10. }

where

在switch语句中作为条件

  1. switch point {
  2. case let (x, y) where x == y:
  3. print("x == y")
  4. case let (x, y) where x == -y:
  5. print("x == -y")
  6. default:
  7. break
  8. }

在for循环中作为条件

  1. // 打印区间内所有的偶数
  2. for i in -100...100 where i%2 == 0 {
  3. print("i = \(i)")
  4. }

标签语句

  1. outer: for i in 0...3 {
  2. for k in 0...3 {
  3. if k == 3 {
  4. continue outer
  5. }
  6. if i == 3 {
  7. break outer
  8. }
  9. print("i = \(i), k = \(k)")
  10. }
  11. }