if - else
- The parentheses can be omitted for the condition after if
- The braces after the condition cannot be omitted
let width = 10
if width >= 30 {
print("宽大于等于30")
}else if width >= 20 {
print("宽大于等于20")
}else if width >= 10{
print("宽大于等于10")
}else{
print("宽小于10")
}
- The condition after if can only be of type Bool
while
var num = 5
while num > 0 {
print("num = \(num)")
num -= 1
}//打印了5次
num -- is not used here because starting from Swift 3, the auto-increment (++) and auto-decrement (–) operators have been removed.
- repeat-while is equivalent to do-while in C language
var num = -1
repeat {
print("num = \(num)")
} while num > 0 //打印了一次
for
- Closed interval operator: a...b , a <= value <= b
let nums = [1,2,3,4,5,6,7,8]
for i in 0...4 {
print(nums[i])
}//1 2 3 4 5
let range = 1...4
for i in range {
print(nums[i])
}//2 3 4 5
let a = 2
let b = 4
for i in a...b {
print(nums[i])
}//3 4 5
for i in a...5 {
print(nums[i])
}//3 4 5 6
// i默认是let,有需要时可以声明为var
for var i in 1...4{
i += 5
print(i)
} //6 7 8 9
//i 在不使用时,可以用 _ 代替
for _ in 1...3{
print("输出")
} //打印了3次
- Half-open interval operator (the header is included but the tail is not included): a...<b , a <= value < b
for i in 0..<5{
print(i)
}//0 1 2 3 4
for - interval operator used on arrays
let nums = [1,2,3,4,5,6,7,8]
for num in nums[0...3] {
print(num)
} //1 2 3 4
- One-sided interval: Make the interval as far as possible in one direction
for num in nums[5...] {
print(num)
}//6 7 8
for num in nums[...2] {
print(num)
}//1 2 3
for num in nums[..<2] {
print(num)
}//1 2
let range = ...3
range.contains(2) //true
range.contains(-2) //true
range.contains(5) //false
interval type
let range1: ClosedRange<Int> = 1...3
let range2: Range<Int> = 1..<3
let range3: PartialRangeThrough<Int> = ...3
- Interval operators can also be used for characters and strings, but they cannot be used in for -in by default.
let stringRange1 = "cc"..."ff" // ClosedRange<String> cd cf dz ....
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到~囊括了所有可能要用到的ASSCII字符
let characterRange : ClosedRange<Character> = "\0"..."~"
characterRange.contains("G") //true
Interval values with intervals
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
switch
- Curly brackets { } cannot be written after case and default
var num = 1
switch num {
case 1:
print("1")
break
case 2:
print("2")
break
default:
print("default")
break
}// 1
- By default, you do not need to write break , and it will not run through to subsequent conditions.
var num = 1
switch num {
case 1:
print("1")
case 2:
print("2")
default:
print("default")
}// 1
fallthrough
- Use fallthrough to achieve penetration effects
var num = 1
switch num {
case 1:
print("1")
fallthrough
case 2:
print("2")
default:
print("default")
}
// 1
// 2
switch note
- The switch must ensure that it can handle all situations
- There must be at least one statement after case and default
- If you don’t want to do anything, just add a break
var num = 1
switch num {
case 1:
print("1")
fallthrough
case 2:
print("2")
default:
break
}
- If you can ensure that all situations have been handled, you do not need to use default .
enum Answer {
case right, wrong}
let answer = Answer.right
switch answer {
case Answer.right:
print("right")
case Answer.wrong:
print("wrong")
}
// 由于已确定answer是Ansewer类型,因此可以省略Ansewer
enum Answer {
case right, wrong}
let answer = Answer.right
switch answer {
case .right:
print("right")
case .wrong:
print("wrong")
}
compound condition
- switch also supports Character and String types
let string = "Jack"
switch string {
case "Jack":
fallthrough
case "Rose":
print("Right person")
default:
break
}//Right person
let string = "Jack"
switch string {
case "Jack", "Rose":
print("Right person")
default:
break
}//Right person
let character: Character = "a"
switch character {
case "a","A":
print("The letter A")
default:
print("Not the letter A")
}// The letter A
Interval matching, tuple matching
//区间匹配
let count = 55
switch count {
case 0:
print("0")
case 1...5:
print("1-5")
case 5..<12:
print("5-11")
case 12..<100:
print("12-99")
case 100..<1000:
print("100-999")
default:
print("many")
}//12-99
//元组匹配
let point = (1, 1)
switch point {
case (0, 0):
print("0 - 0")
case (_, 0):
print("x - 0")
case (0, _):
print("0 - y")
case (-2...2, -2...2):
print("inside the box")
default:
print("outside of the box")
}//inside the box
- Underscore can be used to ignore a value
- Regarding the case matching problem, it belongs to the category of pattern matching (Pattern Matching)
value binding
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 an 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 can also be changed to var if necessary
where
let point = (1, -1)
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")
}//on the line x == -y
//where 可以与 for 循环一起使用
//将所有正数加起来
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
label statement
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)")
}
}