Swift流程控制

2020-09-30  本文已影响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")
}

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次

for

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

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
    
}
var number = 1
switch number {
case 1:
    print("number is 1")
case 2:
    print("number is 2")
default:
    print("number is other")    
}

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注意点

var number = 1
switch number {
case 1:
    print("number is 1")
case 2:
    print("number is 2")
default:
    break
}
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

值绑定

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

猜你喜欢

热点阅读