流程控制

This commit is contained in:
2021-12-03 16:18:52 +08:00
parent 34ce698875
commit 6df15c5e4e
3 changed files with 191 additions and 0 deletions

View File

@@ -0,0 +1,190 @@
//: [Previous](@previous)
//: ##
//: ###
/*:
a...b : a <= <= b
*/
let names = ["Anna", "Alex", "Brian", "Jack"]
for i in 0...3 {
print(names[i])
}
let a = 1
var b = 3
for i in a...b {
print(names[i])
}
let range = 1...3
for i in range {
print(names[i])
}
// iletvar
for var i in 1...3 {
// i
i += 5
print(i)
}
// ,_
for _ in 1...3 {
print("123456")
}
//: ###
/*:
a..<b : a <= < b
*/
//: ###
/*:
a... : a <=
...b : <= b
*/
//: ###
// ClosedRange
let range1: ClosedRange<Int> = 1...3
// Range
let range2: Range<Int> = 1..<3
// PartialRangeThrough
let range3: PartialRangeThrough<Int> = ...5
//: ###
// 使stride
for tickMark in stride(from: 4, to: 11, by: 2) {
print(tickMark)
}
//: ## switch
/*:
switch
- switch
- default
- switch CharacterString
- switch
*/
// break穿
var num = 1
switch num {
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")
}
//: ####
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 x-axis")
case (0, _):
print("on the y-axis")
case (-2...2, -2...2):
print("inside the box")
default:
print( "outside of the box")
}
//: ###
// let var
let point1 = (2, 0)
switch point1 {
case (let x, 0):
print("on the x-axis with a x value 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
// 1. case
let point2 = (1, -1)
switch point2 {
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("somewhere else at (\(x), \(y))")
}
//2.
var numbers = [10, 20, 30, -10, -30]
var sum = 0
for num in numbers where num > 0 {
sum += num;
}
print(sum)
//: ###
//
// :
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)")
}
}
//: [Next](@next)

View File

@@ -3,5 +3,6 @@
<pages>
<page name='01 注释'/>
<page name='02 数据类型'/>
<page name='03 流程控制'/>
</pages>
</playground>