Introduction to swift practice output Chapter5

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.

branch depending on the success or failure of the if statement condition

chapter5.swift


let value = 5

if value <= 4 {
    print("value is 4 or less")
}else {
    print("value is 5 or more")
}

if let statement Branch depending on the presence or absence of a value

Optional types are data types that allow variables to be assigned nil, while non-optional types cannot be assigned nil. Optional variables have a "?" Or "!" At the end of the data type.

chapter5.swift


let optionalA = Optional(1)

if let a = optionalA {
    print("value is\(a)is")
}else {
    print("Value does not exist")
}

let optionalB = Optional("b")
let optionalC = Optional("c")

if let b = optionalB, let c = optionalC{
    print("value is\(b)When\(c)you know")
}else {
    print("Absent")
}

guardl statement Branch that leaves early when conditions are not met

The guard statement is a syntax that describes what to do if the conditions are not met. If the condition is not met, it is necessary to write a process to end the method, end the iterative process, or exit the scope.

Code that exits processing when the condition is not met using the guard statement

chapter5.swift


//guard conditional expression else{
//Statement executed when the conditional expression is false
//You need to exit to the scope statement where the guard statement is written
//}

func someFunction() {
    let value = -1
    
    guard value >= 0 else {
        print("Value less than 0")
        return
    }
    print(value)
}
someFunction()

Forced exit from the scope of the guard statement

chapter5.swift


func printIfPositive(_ a: Int) {
    guard a > 0 else {
        return
    }
    print(a)
}
printIfPositive(6)

Access to variables and constants declared in the guard statement

What is the difference between a guard let statement and an if let statement? Variables and constants declared in the guard let statement can be used after the guard let statement.

chapter5.swift


func someFONCTION() {
    let a :Any = 3
    
    guard let int = a as? Int else {
        print("a is not an Int type")
        return
    }
    print("The value is of type Int\(int)is")
}
someFONCTION()

func checkInt(num:Int) {
    guard num >= 0 else {
        print("It's a minus")
        return
    }
    print("Is a plus")
}
 
checkInt(num:-10)
checkInt(num:10)

Switch statement Branch by multiple pattern matching

chapter5.swift


let r = -1
switch  r {
case Int.min ..< 0:
    print("r is a negative number")
    
case 1 ..< Int.max:
    print("r is a positive number")
default:
    print("a is 0")
}

Checking the completeness of the case

chapter5.swift


enum SomeEnum {
    case foo
    case bar
    case baz
}

let foo = SomeEnum.foo

switch foo {
case .foo:
    print(".foo")
    
case .bar:
    print(".bar")
    
case .baz:
    print(".baz")

}

where keyword Add conditions that match the case

chapter5.swift


let optionalP: Int? = 12

switch optionalP {
case .some(let a) where a > 10:
    print("Value greater than 10\(a)Exists")
default:
    print("Value does not exist or is less than 10")
}

let g = 1

switch g {
case 1:
    print("Will be executed")
    break
    print("Not executed")
default:
    break
}

Recommended Posts

Introduction to swift practice output Chapter5
Introduction to swift practice output Chapter 5 Part 2
[Note] About the introduction to Swift practice Chapter 11
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
Output of the book "Introduction to Java"
Introduction to Ruby 2
Introduction to SWING
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Introduction to java
Introduction to Doma
Java Performance Chapter 1 Introduction
Introduction to JAR files
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
[Practice! ] Introduction of JFrame (explaining up to screen creation)
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]
Introduction to Design Patterns (Builder)
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to Android application development
Introduction to RSpec 3. Model specs
Introduction to Ratpack (5) --Json & Registry
Introduction to Metabase ~ Environment Construction ~
Introduction to Ratpack (7) --Guice & Spring
(Dot installation) Introduction to Java8_Impression
Introduction to Design Patterns (Composite)
[Swift] How to use UserDefaults
How to use Swift UIScrollView
Introduction to Micronaut 2 ~ Unit test ~
Output javadoc to word file
Introduction to JUnit (study memo)
Introduction to Spring Boot ① ~ DI ~
Introduction to design patterns (Flyweight)
[Java] Introduction to lambda expressions
Introduction to Spring Boot ② ~ AOP ~
Introduction to Apache Beam (2) ~ ParDo ~
[Ruby] Introduction to Ruby Error statement
Output XML tree to file
Introduction to EHRbase 2-REST API
Practice to unify Dialog implementation
Introduction to design patterns Prototype
[Swift] Processing to share screenshots