美文网首页
Swift流程控制

Swift流程控制

作者: 得_道 | 来源:发表于2020-09-30 09:59 被阅读0次

概览

if-else
while
for
for-区间运算符用在数组上
switch

if-else

let age = 23
if age >= 22 {
    print("get married")
} else if age > 18 {
    print("bing a adult")
} else if age >= 7 {
    print("go to school")
} else {
    print("just a child")
}
  • if后面可以省略小括号

  • 条件后面的大括号不可省略

  • if后面的条件只能是bool类型


    image.png

while

var num = 5
while num > 0 {
    print("num is \(num)")
    num -= 1
} //打印5次
var num2 = -1
repeat {
    print("num2 is \(num2)")
} while num2 > 0 //打印1次
  • repeat-while 相当于C语言中的do-while
  • 这里不能用num--,是因为
  • 从Swift3开始,去除了自增(++),自减(--)运算符

for

  • 闭区间运算符:a...b, a <= 取值 <= b
let names = ["aaa","bbb", "ccc"]
for i in 0...2 {
    print(names[i])
}
// i默认是let,有需要时可以声明为var
for var i in 1...3 {
    i += 5
    print(i)
}
let range = 1...2
for i in range {
    print(names[i])
}
for _ in 1...2 {
    print("for")
}
  • 半开区间运算法:a..<b, a <= 取值 < b
for i in 1..<5 {
    print(i)
}

for-区间运算符用在数组上

let names = ["aaa","bbb", "ccc", "ddd"]
for name in names[0...3] {
    print(name)
}
  • 单侧区间:让区间朝一个方向尽可能远
for name in names[2...] {
    print(name)
}

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

for name in names[..<2] {
    print(name)
}
let range1 = ...5
range1.contains(7)//false
range1.contains(4)//true
range1.contains(-3)//true

Switch

  • case、 default后面不能写{}
var number = 1
switch number {
case 1:
    print("number is 1")
    break
case 2:
    print("number is 2")
    break
    
default:
    print("number is other")
    break
    
}
  • 默认可以不写break,并不会贯串到后面的条件
var number = 1
switch number {
case 1:
    print("number is 1")
case 2:
    print("number is 2")
default:
    print("number is other")    
}

fallthrough

  • 使用fallthrough可以实现贯串效果
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

switch注意点

  • 1、switch必须保证能处理所有情况


    image.png
  • 2、case、default后面至少要有一条语句,如果不想做任何事加个break即可

var number = 1
switch number {
case 1:
    print("number is 1")
case 2:
    print("number is 2")
default:
    break
}
  • 3、如果能保证处理所有情况,也可以不必加default
let anwser = Anwser.right
switch anwser {
case Anwser.right:
    print("right")
case Anwser.wrong:
    print("wrong")
}

复合条件(多个case放一起)
switch也支持Character,String类型

let string = "Jack"
switch string {
case "Jack", "Rose":
print("Rose person")
default:
    break
}
let character: Character = "a"
switch character {
case "a", "A":
    print("the letter A")
default:
    print("not the letter a")
}

区间匹配、 元祖匹配

let count = 50
switch count {
case 0:
    print("none")
case 1..<5:
    print("a few")
case 5..<12:
    print("several")
case 12..<100:
    print("dozens 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
  • 可以使用下划线_,忽略某个值
  • 关于case匹配问题,属于模式匹配的范畴(Pattern Matching)

值绑定

let point = (2,2)
switch point {
case (let x, 0):
    print("on the x-axis with an x calue 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))")
}

where

let point = (2,2)
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")
}
var numbers = [10, 20, -10, -20, 30, -30]
var sum = 0
for num in numbers where num > 0 {
    sum += num
}

相关文章

网友评论

      本文标题:Swift流程控制

      本文链接:https://www.haomeiwen.com/subject/wlveuktx.html