Swift5.0笔记 - 2. 流程控制

2020-10-21  本文已影响0人  南城同學

1. if-else

let age = 4
if age >= 22 {
}

2. while

var num = 5;
while num > 0 {
    print("num is \(num)")
    num -= 1
}//打印了5次
var num01 = 5;
repeat {
    print("num is \(num01)")
} while num01 > 0 //打印了1次

3. for

3.1
let name = ["Ann", "Toom", "Alex", "Jack"]
for i in 0...3 {
     print(name[i])
}

//二
let range = 1...3
for i in range {
      print(names[i])
}

//三
let a = 1
var b = 2
for i in a...b {
    print(names[i])
}

//四
for i in a...3 {
      print(names[i])
}

//i默认是let, 有需要时可以声明var
for var i in 1..3 {
}
//如果用不到“i”, 用“_”
for _ in 1...3 {
}
let names = ["Ann", "Cbb", "Edd", "Gaa"]
//从角标“2”开始,到最大“3”结束
for name in names[2...] {
      print(name)
}

//从最小开始,到“2”结束
for name in names[...2] {
      print(name)
}

//从最小开始,到“1”结束
for name in names[..<2] {
      print(name)
}

//数组之外的
//从负无穷,到5
let range = ...5


3.2 for - 区间运算符用在数组上
let names = ["Ann", "Cbb", "Edd", "Gaa"]
for name in names[0...3] {
      print(name)
}

4. 区间类型

4.1 不带间隔的区间类型
let range1: ClosedRange<Int> = 1...3
let range2: Range<Int> = 1..<3
let range3: PartialRangeThrough<Int> = ...3

let stringRange1 = "cc"..."fff" 
stringRange1.contains("cb") //false
stringRange1.contains("dz") //true

let stringRange2 = "a"..."f" //
stringRange2.contains("h") //false
stringRange2.contains("c") //true

let characterRange3: ClosedRange<Character> = "\0"..."~"
4.2 带间隔的区间类型
let hours = 11
let hourInterval = 2
//tickMark的取值:从4开始,累加2,不超过11
for tickMark in stride(from: 4, though: hours, by: hourInterval) {
   print(tickMark)
} // 4,6, 8, 10

5. switch

var num = 1
switch num {
case 1:
     print("num is 1")
     break
case 2:
     print("num is 2")
     break
default:
     print("num is other")
     break
} // 打印“1”
var num = 1

//这种会提示“switch must be exhaustive”
switch num {
case 1:
     print("num is 1")
case 2:
     print("num is 2")
} 

enum Answer {case right, wrong}
let answer = Answer.right
switch answer {
case Answer.right:
     print("right")
case Answer.wrong:
     print("wrong")
}

5.2 fallthough
var num = 1
switch num {
case 1:
     print("num is 1")
     fallthough
case 2:
     print("num is 2")
default:
     print("num is other")
}
//打印 “1”、“2”
5.3 复合条件
let string = "Jack"
switch string {
case "Jack":
     fallthough
case "Rose":
     print("Right person")
default:
     break
}
switch string {
case "Jack", "Rose":
     print("Right person")
default:
     break
}

6. 区间匹配、元组匹配

let count = 30
switch count {
case 0:
    print("none")
case 1..<5:
    print("a few")
default:
    break
}
let point = (1, 1)
switch point {
case (0, 0):
    print("0, 0")
case(0,_):
    print("on the y-axis")
case(-2...2, -2...2):
    print("inside the box")
default:
    break
}

7. 值绑定

let point01 = (2, 0)
switch point01 {
case (let x, 0):  // 条件满足,进入
    print("on the x-axis with an x value of \(x)")
case (0, let y):
    print("on the x-axis with an x value of \(y)")
case let(x, y):
    print("somewhere else at(\(x), \(y)) ")
}

打印:“on the x-axis with an x value of 2”


8. Where

let point02 = (1, -1)
switch point02 {
case let(x,y) where x == y:
    print("on the line x == y")
case let(x,y) where x == -y: //当“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 num0 in numbers where num0 > 0 {
    sum += num0
}
print(sum)//60

9. 标签

outer: for m in 1...4 {
    for k in 1...4 {
        if k == 3 {
            continue outer
        }
        if m == 3 {
            break outer
        }
        print("m == \(m), k == \(k)")
    }
}

打印:

m == 1, k == 1
m == 1, k == 2
m == 2, k == 1
m == 2, k == 2

上一篇下一篇

猜你喜欢

热点阅读