Conditional branching, repetition, delayed execution, pattern matching Syntax that controls the execution flow of a program. There are conditional branching and repetition in the control of the control flow, and they can be combined and executed freely.
chapter5.swift
let array = [1,2,3]
for element in array {
print(element)
}
Dictionary
chapter5.swift
let dictionary = ["a": 1, "b": 2]
for (key,value) in dictionary {
print("Key: \(key), vlue: \(value)")
}
chapter5.swift
var f = 1
while f < 4 {
print(f)
f += 1
}
Since the while statement evaluates the conditional expression before execution, it may never be executed. Therefore, if you want to repeat it more than once regardless of the success or failure of the conditional expression, use repeat whie.
chapter5.swift
var t = 1
repeat {
print(t)
t += 3
}
while t < 1
The break statement ends the entire repeating statement.
chapter5.swift
var containsTwo = false
let garray = [1,2,3]
for element in garray {
if element == 2 {
containsTwo = true
break
}
print("element:\(element)")
}
print("containsTwo:\(containsTwo)")
chapter5.swift
var odds = [Int]()
let rarray = [1,2,3,4,5,6]
for element in rarray {
if element % 2 == 1 {
odds.append(element)
continue
}
print("even:\(element)")
}
print("odds:\(odds)")
}
defer statement: Processing when leaving the scope
chapter5.swift
var count = 0
func someFUNTIONS() -> Int{
defer {
count += 1
}
return count
}
print(someFUNTIONS())
print(someFUNTIONS())
print(count)
swift has the concept of a pattern that expresses the structure and properties of a value Waiting for a pattern is to check if the value matches a specific pattern.
Expression pattern ~ = Evaluation by operator
chapter5.swift
let integer = 6
switch integer {
case 6:
print("match: 6")
case 5...10:
print("match: 5...10")
default:
print("default")
}
chapter5.swift
let values = 3
switch values {
case let matchValue :
print(matchValue)
}
chapter5.swift
let values = 3
let optionalD = Optional(4)
switch optionalD{
case let f?:
print(f)
default:
print("nil")
}
}
chapter5.swift
enum Hemisphere {
case northern
case southern
}
let hemisphere = Hemisphere.southern
//let hemisphere = Optional(Hemisphere.southern)
switch hemisphere {
case .northern:
print("match: .northern")
case .southern:
print("match: .southern")
//case nil:
// print("nil")
}
enum Color {
case rgb(Int,Int,Int)
case cmyk(Int,Int,Int,Int)
}
let color = Color.rgb(100, 200, 230)
switch color {
case .rgb(let r, let h, let j):
print(".rgb: (\(r), \(h), \(j))")
case .cmyk(let k,let y, let u, let o):
print(" .cmyk: (\(k),\(y),\(u),\(o))")
}
chapter5.swift
let any: Any = "fafafa"
switch any {
case is String:
print("match: String")
case is Int:
print("match: Int")
default:
print("default")
}
}
chapter5.swift
let anys : Any = "1"
switch anys {
case let string as String:
print("match: string(\(string))")
case let int as Int:
print("match: int(\(int))")
default:
print("default")
}
chapter5.swift
let fff = 9
if case 1...10 = fff {
print("yes")
}
//guard statement
func someFunc() {
let value = 9
guard case 1...10 = value else {
return
}
print("yes")
}
someFunc()
//for in statement
let harray = [1,2,3,4,5,6]
for case 2...5 in harray {
print("2 or more and 5 or less")
}
chapter5.swift
var nextValue = Optional(1)
while case let value? = nextValue {
print("value:\(value)")
if value >= 8 {
nextValue = nil
} else {
nextValue = value + 1
}
}
Recommended Posts