Introduction to swift practice output Chapter 5 Part 2

Control syntax

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.

for-in statement enumeration of sequence elements

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)")
}

while statement repeats according to continuation conditions

chapter5.swift


var f = 1
while f < 4 {
    print(f)
    f += 1
}

repeat while statement

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

Suspended during execution

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)")

The continue statement suspends only the current processing and continues the subsequent processing.

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 for delayed execution

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)

Evaluation based on the structure and nature of pattern matching values

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")
}

Value binding Evaluation with value substitution

chapter5.swift


let values = 3

switch values {
case let matchValue :
    print(matchValue)
}

Evaluation of the presence or absence of an optional type value

chapter5.swift


let values = 3
let optionalD = Optional(4)
switch optionalD{
case let f?:
    print(f)
    
default:
    print("nil")
}

}

Enumeration type case pattern Evaluation of match with case

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))")

}

Type casting pattern by is operator Evaluation by type judgment

chapter5.swift


let any: Any = "fafafa"
switch any {
case is String:
    print("match: String")
case is Int:
    print("match: Int")
default:
    print("default")
}

}

Type casting pattern by as operator Evaluation by type casting

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")
}

Where pattern matching can be used if statement

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")
}

while statement

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

Introduction to swift practice output Chapter 5 Part 2
Introduction to swift practice output Type representing the Chapter 4 collection Part 1
Introduction to swift practice output Type representing the Chapter 4 collection Part 2
[Note] About the introduction to Swift practice Chapter 11
Introduction to Spring Boot Part 1
Introduction to Linux Container / Docker (Part 2)
Output of the book "Introduction to Java"
Introduction to Ruby 2
Introduction to SWING
Road to Java Engineer Part1 Introduction & Environment Construction
java practice part 1
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Introduction to java
forEach practice (Part 1: Rewrite from forEach statement to forEach method)
Introduction to Doma
Introduction to algorithms in java-lexicographic order / combination omnivorous (part1)
[Practice! ] Introduction of JFrame (explaining up to screen creation)
Java Performance Chapter 1 Introduction
Introduction to JAR files
Introduction of Docker --Part 1--
Introduction to Ratpack (8)-Session
Introduction to RSpec 1. Test, RSpec
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to design patterns (introduction)
Introduction to Practical Programming
Introduction to javadoc command
Introduction to jar command
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to java command
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Introduction to javac command
[Xcode 12.3] Introduction to Apple Platform App Development-Object Edition- [Swift UI]