文章目录
- Swift - 流程控制
- if-else
- 2. while
- 3. for
- 3.1 闭区间运算符
- 3.2 半开区间运算符
- 3.3 for - 区间运算符用在数组上
- 3.3.1 单侧区间
- 3.4 区间类型
- 3.5 带间隔的区间值
- 4. switch
- 4.1 fallthrough
- 4.2 switch注意点
- 5. 复合条件
- 6. 区间匹配、元组匹配
- 7. 值绑定
- 8. where
- 9. 标签语句
Swift - 流程控制
if-else
- if后面的条件可以省略小括号
- 条件后面的大括号不可以省略
let age = 4
if age >= 22 {
print("Get married")
} else if age >= 18 {
print("Being a adult")
} else if age >= 7 {
print("Go to school")
} else {
print("Just a child")
}
- if后面的条件只能是Bool类型
2. while
repeat-while
相当于C语言中的do-while
- 这里不用num–,是因为
-
- 从
Swift3
开始,去除
了自增(++)
、自减(--)
运算符
- 从
var num1 = 5
while num1 > 0 {
print("num is \(num1)")
num1 -= 1
} //打印了5次
var num2 = -1
repeat {
print("num is \(num2)")
} while (num2 > 0) //打印了1次
3. for
3.1 闭区间运算符
a…b,a <= 取值 <= b
遍历一个闭区间:
let names = ["Anna", "Alex", "Brian",
"Jack"]
for i in 0...3 {
print(names[i])
} // Anna Alex Brian Jack
闭区间赋值给一个常量
let range = 1...3
for i in range {
print(names[i])
} // Alex Brian Jack
使用变量表示区间的开头、结果
let a = 1
let b = 2
for i in a...b {
print(names[i])
} // Brian Jack
for i in a...3 {
print(names[i])
} // Alex Brian Jack
i
默认是let,有需要是可以声明为var
for var i in 1...3 {
i += 5
print(i)
} //6 7 8
没有用到
i
,建议写个下划线_
for _ in 1...3 {
print("123")
}
3.2 半开区间运算符
a…<b,a <= 取值 < b
// 半开区间运算符
for i in 1..<5 {
print(i)
} // 1 2 3 4
3.3 for - 区间运算符用在数组上
// for - 区间运算符用在数组上
for name in names[0...3] {
print(name)
} // Anna Alex Brian Jack
3.3.1 单侧区间
单侧区间:让区间朝一个方向尽可能的远
// 单侧区间:让区间朝一个方向尽可能的远
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 range2 = ...5
range2.contains(7) // false
range2.contains(4) // true
range2.contains(-3) // true
3.4 区间类型
前面写的区间并没有写类型,编译器自动推断类型了。
区间类型
// 区间类型
let range1: ClosedRange<Int> = 1...3
let range2: Range<Int> = 1..<3
let range3: PartialRangeThrough<Int> = ...5
字符、字符串也能使用区间运算符,但默认不能用在for-in中
// 字符、字符串也能使用区间运算符,但默认不能用在for-in中
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
写明类型
ClosedRange<Character>
//\0到~囊括了所有可能要用到的ASCII字符
let characterRange: ClosedRange<Character> = "\0"..."~"
characterRange.contains("G") //true
3.5 带间隔的区间值
// 带间隔的区间值
let hours = 11
let hourInterval = 2
// tickMark的取值:从4开始,累加2,不超过11
for tickMark in stride(from: 4, through: hours, by: hourInterval) {
print(tickMark)
} // 4 6 8 10
4. 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
} // number is 1
默认可以不写
break
,并不会贯穿
到后面的条件
var number = 1
switch number {
case 1:
print("number is 1")
case 2:
print("number is 2")
default:
print("number is other")
} // number is 1
4.1 fallthrough
使用
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
4.2 switch注意点
switch必须要保证能处理所有情况
case
、default
后面至少
要有一条语如果不想做任何事,加个
break
即可
如果能保证已处理
所有
情况,也可以不必使用default
// 如果能保证已处理`所有`情况,也可以不必使用
enum Answer { case right, wrong}
let answer = Answer.right
switch answer {
case Answer.right:
print("right")
case Answer.wrong:
print("wrong")
}
由于已确定answer是Ansewer类型,因此可以省略Answer
// 由于已确定answer是Ansewer类型,因此可以省略Answer
switch answer {
case .right:
print("right")
case .wrong:
print("wrong")
}
5. 复合条件
- switch也支持Character、String类型
实现复合条件
这样写汇报case
至少需要一条语句,需要写个fallthrough
// switch也支持Character、String类型
let string = "Jack"
switch string {
case "Jack":
fallthrough
case "Rose":
print("Right person")
default:
break
}
也可以使用
,
实现复合条件
switch string {
case "Jack", "Rose":
print("Right person")
default:
break
}
Character
let character: Character = "a"
switch character {
case "a", "A":
print("The letter A")
default:
break
}
6. 区间匹配、元组匹配
区间匹配
let count = 62
switch count {
case 0:
print("none")
case 1..<5:
print("a few")
case 5..<12:
print("several")
case 12..<100:
print("dozens of")
case 100..<1000:
print("hundreds of")
default:
print("many")
} // dozens of
元组匹配(坐标系)
// 元组匹配(坐标系)
let point = (1, 1)
switch point {
case (0, 0):
print("the origin")
case (_, 0):
print("on the y-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)的范畴,以后会再次详细展开讲解
7. 值绑定
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-asix with a y value of \(y)")
case let (x, y):
print("somewhere else at \(x), \(y)")
} // on the x-axis with an x value of 2
必要时let也可以改为var
8. where
坐标
// 坐标
let point3 = (1, -1)
switch point3 {
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")
} // on the line x == -y
将所有正数加起来
// 将所有正数加起来
var numbers = [10, 20, -10, -20, 30, -30]
var sum = 0
for num in numbers where num > 0 {
sum += num
}
print(sum) // 60
9. 标签语句
如下代码,正常情况我们在
for k in
里面使用continue
、break
,针对的是for k in
这个循环,这里我们使用标签语句
,实现在for k in
里面控制for i in
这一层循环
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)")
}
}
/* 打印内容
i == 1, k == 1
i == 1, k == 2
i == 2, k == 1
i == 2, k == 2
*/
@oubijiexi