let age =4 if age >=22 { print("Get married") } elseif age >=18 { print("Being a adult") } elseif age >=7 { print("Go to school") } else { print("Just a child") }
while
1 2 3 4 5 6 7 8 9 10
var num =5 while num >0 { print("num is \(num)") num -=1 }// 打印了5次
var num =-1 repeat { print("num is \(num)") } while num >0//打印了一次
let names = ["Anna", "Alex", "Brian", "Jack"] for name in names[0...3] { print(name) } // Anna Alex Brian Jack
单侧区间:让区间朝一个方向尽可能的远 for name in names[2...] { print(name) } // Brian Jack for name in names[...2] { print(name) } // Anna Alex Brian for name in names[..<2] { print(name) } // Anna Alex
let range =...5 range.contains(7) // false range.contains(4) // true range.contains(-3) // true
区间类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
let range1: ClosedRange<Int> =1...3 let range2: Range<Int> =1..<3 let range3: PartialRangeThrough<Int> =...5
let hours =11 let horInterval =2 // tickMark的取值:从4开始,累加2,不超过11 for tickMark instride(from: 4, to: hours, by: horInterval) { print(tickMark) }// 4 6 8 10
switch
1 2 3 4 5 6 7 8 9
var number =1 switch number { case1: print("number is 1") case2: print("number is 2") default: print("number is other") }// number is 1
默认可以不写break,并不会贯穿到后面的条件
case,default后面不能写大括号{}
fallthrough
使用fallthrough可以实现贯穿效果
1 2 3 4 5 6 7 8 9 10 11
var number =1 switch number { case1: print("number is 1") fallthrough case2: print("number is 2") default: print("number is other") } // number is 1 // number is 2
switch注意点
switch必须要保证能处理所有情况
case、default后面至少要有一条语句
如果不想做任何事,加个break即可
1 2 3 4 5 6 7 8 9
var number =1 switch number { case1: print("number is 1") case2: print("number is 2") default: break }
如果能保证已处理所有情况,也可以不必使用default
1 2 3 4 5 6 7 8 9 10 11 12 13 14
enumAnswer { case right, wrong } let answer =Answer.right switch answer { caseAnswer.right: print("right") caseAnswer.wrong: print("wrong") } // 由于已确定answer是Ansewer类型,因此可以省略Answer switch answer { case .right: print("right") case .wrong: print("wrong") }
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)的范畴
值绑定
1 2 3 4 5 6 7 8 9
let point = (2, 0) switch point { case (let x, 0): print("on the x-axis with an x value of \(x)") case (0, let y): print("on the y-axis with a y value of \(y)") caselet (x, y): print("somewhere else at (\(x), \(y))") } // on the x-axis with an x value of 2
必要时let也可以改为var
where
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
let point = (1, -1) switch point { caselet (x, y) where x == y: print("on the line x == y") caselet (x, y) where x ==-y: print("on the line x == -y") caselet (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 num in numbers where num >0 { // 使用where来过滤num sum += num } print(sum) // 60
标签语句
1 2 3 4 5 6 7 8 9 10 11
outer: for i in1...4 { for k in1...4 { if k ==3 { continue outer } if i ==3 { break outer } print("i == \(i), k == \(k)") } } } }