04-Swift条件控制

2020-12-12  本文已影响0人  一抹相思泪成雨

1.if-else

使用方式:

let age = 4
if age >= 22 {
    print(">= 22")
}else if age >= 18 {
    print(">= 18")
}else if age >= 7{
    print(">= 7")
}else{
    print("other")
}

2.while

使用方式:

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

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

3.for

使用方式:

let names = ["小明","小红","小张","小白"]
/// for in  半开区间
for i in 0 ..< 3 {
   print(names[i])
}

for items in names {
   print(items)
}
/// for in enumerated
for (index, value) in names.enumerated() {
   print("index =", index, "value =", value)
}

/// forEach
names.forEach { (item) in
   print(item)
}

// $0 变量
names.forEach { 
   print($0)
}

/// stride to (不包含)
for index in stride(from: 1, to: 4, by: 1) {
   print(index)
}

/// stride through (包含)
for index in stride(from: 0, through: 4, by: 2) {
   print(index)
}
3.1 for – 区间运算符用在数组上
for name in names[2...] { // 单侧区间,让区间朝一个方向尽可能的远
   print(name)
}
for name in names[...2] {
   print(name)
}
for name in names[..<2] {
   print(name)
}
3.2 i默认是let,需要也可以声明为var
//: i默认是let,需要也可以声明为var
for var i in 1...3 { // 闭区间包含 1和3
   i += 5 // 操作i 需变为var类型
   print(i)
}/// 6 7 8

4.range的使用

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

let range = ...5 
range.contains(7) // false 
range.contains(4) // true
range.contains(-3) // true

let stringRange1 = "cc"..."ff" // 
ClosedRange<String> stringRange1.contains("cb") // false 
stringRange1.contains("dz") // true 
stringRange1.contains("fg") // false

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

5.switch

var number = 1
switch number {
case 1:
   print("number is 1")
case 2:
   print("numebr is 2")
default:
   print("number is other")
}

//: fallthrough 可以实现贯穿效果
switch number {
case 1:
   print("number is 1")
   fallthrough
case 2:
   print("number is 2")
default:
   print("number is other")
}

6.复合条件

let string = "Jack"
switch string {
case "Jack", "Rose":
   print("Right person")
default:
   break
} // Right person

let character: Character = "a"
switch character {
case "a", "A":
   print("The letter A")
default:
   print("Not the letter A")
} // The letter A

7.区间匹配、元组匹配

let count = 62
switch count {
case 0:
   print("none")
case 1..<100: // 区间匹配
   print("a few")
case 100..<1000:
   print("handreds of")
default:
   print("many")
}

var 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

8.值绑定

point = (2, 0) // 一个符合就行
switch point {
case (let x, 0):
   print("on the x-axis with an x value of \(x)") // 将x值拿出直接使用
case (0, let y):
   print("on the y-axis with y value of \(y)")
case let(x, y):
   print("somewhere else at(\(x), \(y))")
}

9.where

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")
}

let numbers = [-1,2,5]
var sum = 0;
for num in numbers where num > 0 { // where 来过滤num 相当于 continue
   sum+=num;
}
print(sum)

10.标签语句outer

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)")
   }
}
上一篇下一篇

猜你喜欢

热点阅读