if-else
let age = 10
if age < 18 {
print("未成年")
} else if age == 18 {
print("刚成年")
} else {
print("成年人")
}
- if后面的条件可以省略小括号
- 条件后面的大括号不可以省略
- if后面的条件只能是Bool类型
while
var age = 0
while age < 10 {
age += 1
print(age)
}
var age = 0
repeat {
age += 1
print(age)
} while age < 10
repeat-while相当于C语言中的do-while
for循环
- 闭区间运算符:a...b相当于 a<=取值<=b
for i in 0...3 {
print(i)
} // 打印结果 0 1 2 3
let range = 0...3
for i in range {
print(i)
} // 打印结果 0 1 2 3
let a = 0
for i in a...3 {
print(i)
} // 打印结果 0 1 2 3
// i默认为let,有需要时可以声明为var
for var i in 0...3 {
i += 3
print(i)
} // 打印结果 3 4 5 6
// i如果用不到,可以用下划线代替
for var _ in 0...3 {
print("循环")
}
- 半开区间运算符:a..<b相当于 a<=取值<b
for var i in 0..<3 {
print(i)
} // 打印 0 1 2
-
单侧区间
-
a... 表示从a开始往尽可能大的数去
-
...b 表示从尽可能小的数到b
let range = ...5 // range表示从无穷小到5
print(range.contains(-10)) // true
print(range.contains(4)) // true
print(range.contains(8)) // false
- 区间运算符用在数组上
let arr = ["A", "B", "C", "D"]
for var item in arr[0...2] {
print(item)
} // 打印 A B C
// 0...从0开始往上尽可能的大,但是不会超过数组边界
let arr = ["A", "B", "C", "D"]
for var item in arr[0...] {
print(item)
} // 打印 A B C D
// ...2从尽可能的小的数开始到2,但是不会超出数组边界,也就是从0到2
let arr = ["A", "B", "C", "D"]
for var item in arr[...2] {
print(item)
} // 打印 A B C
- 区间类型
let range1: ClosedRange <Int> = 1...3 // Int型闭区间
let range2: Range<Int> = 1..<3 // Int型半开区间
let range3: PartialRangeThrough<Int> = ...5 // Int型单侧区间
字符字符串也能使用区间运算符但默认不能用在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
// \0到~囊括了所有可能要用到的ASCII字符
let characterRange: ClosedRange<Character> = "\0"..."~"
characterRange.contains("G") // true
- 间隔区间
// tickMark的取值: 从4开始,累加2,不超过11
for tickMark in stride(from: 4, through: 11, by: 2) {
print (tickMark)
} // 打印4 6 8 10
switch
- case default后面不能写大括号{}
var number = 2
switch number {
case 1:
print ("number is 1")
break
case 2:
print("number is 2")
break
default:
print ("number is other")
break
} // 打印:number is 2
- 默认可以不写break,并不会贯穿到后面
var number = 2
switch number {
case 1:
print ("number is 1")
case 2:
print("number is 2")
default:
print ("number is other")
} // 打印:number is 2
- 使用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必须要保证处理了所有可能出现的情况,下面是缺少了default的情况报错
- case和default后面至少要跟一条语句,如果什么都不想做,可以加一个break
var number = 1
switch number {
case 1:
print ("number is 1")
case 2:
break
default:
break
}
- switch也支持Character、String类型
var string = "Jonas"
switch string {
case "Jonas":
print ("my name is Jonas")
case "Bob":
print ("my name is Jonas")
default:
break
} // 打印:my name is Jonas
- 复合条件
var string = "Bob"
switch string {
case "Jonas":
fallthrough
case "Bob":
print ("we are boy")
case "Mary":
print ("i am girl")
default:
break
} // 打印:we are boy
------或者是下面的写法-------
var string = "Bob"
switch string {
case "Jonas", "Bob":
print ("we are boy")
case "Mary":
print ("i am girl")
default:
break
} // 打印:we are boy
- 区间匹配
var num = 8
switch num {
case 0:
print("是0")
case 1..<5:
print ("在大于等于1小于5")
case 5...:
print ("大于等于5")
default:
print("小于0")
} // 打印:大于等于5
-
元组匹配
可以使用下划线忽略某个值,只要一边匹配就行
let point = (2, 5)
switch point {
case (0, 0):
print("坐标是(0, 0)")
case (2, _):
print ("x坐标是2")
case (_, 3):
print ("y坐标是3")
case (0...2, 1...3):
print("在这个区域内")
default:
print("其他")
} // 打印:x坐标是2
- 值绑定
let point = (2, 5)
switch point {
case (0, 0):
print("坐标是(0, 0)")
case (2, let y):
print ("x坐标匹配y坐标是\(y)")
case (let x, 3):
print ("y坐标匹配x坐标是\(x)")
case (0...2, 1...3):
print("在这个区域内")
case let (x, y):
print("x坐标是\(x),y坐标是\(y)")
} // 打印:x坐标匹配y坐标是5
where
可以使用where关键字增加条件
let point = (2, 5)
switch point {
case let (x, y) where x == y:
print("x=y")
case let (x, y) where x == -y:
print ("x=-y)")
case let (x, y) where x == y - 3:
print ("x=y-3")
case let (x, y):
print("x坐标是\(x),y坐标是\(y)")
} // 打印:x=y-3
for i in 0...10 where i > 5 {
print(i)
} // 打印:6,7,8,9,10
标签语句
标签语句格式是一个名字加冒号,如outer:
下面的代码是2层嵌套循环,在最外层循环加一个标签,当内部循环j=3时,break,正常情况下是跳出当前循环也就是内部的j循环,继续执行外层的i循环,但是现在加了break outer,表示跳出outer标签语句的循环,所以整个大的循环都停止了。
outer: for i in 0...10 {
print("i=\(i)")
for j in 0...10 {
print("j=\(j)")
if j == 3 {
break outer
}
}
}
print("循环结束")
网友评论