← Swift EnglishChapter 04 of 13

Control Flow

## Learning Objectives - Master if-else statements - Work with switch statements - Use loops (for, while, repeat-while) - Understand guard statements - Learn control transfer statements ## If Statements ### Basic If ```swift let score = 85 if score >= 60 { print("Passed!") } ``` ### If-Else ```swift let age = 20 if age >= 18 { print("Adult") } else { print("Minor") } ``` ### If-Else If ```swift let grade = 85 if grade >= 90 { print("A") } else if grade >= 80 { print("B") } else if grade >= 70 { print("C") } else if grade >= 60 { print("D") } else { print("F") } ``` ### Nested If ```swift let x = 10 if x > 0 { if x < 100 { print("x is between 0 and 100") } } ``` ## Switch Statements ### Basic Switch ```swift let day = 3 switch day { case 1: print("Monday") case 2: print("Tuesday") case 3: print("Wednesday") case 4: print("Thursday") case 5: print("Friday") case 6, 7: print("Weekend") default: print("Invalid day") } ``` ### Multiple Values ```swift let character = "a" switch character { case "a", "e", "i", "o", "u": print("Vowel") case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z": print("Consonant") default: print("Not a letter") } ``` ### Range Matching ```swift let score = 85 switch score { case 90...100: print("A") case 80..<90: print("B") case 70..<80: print("C") case 60..<70: print("D") default: print("F") } ``` ### Value Binding ```swift let point = (2, -3) switch point { case (let x, 0): print("On x-axis at \(x)") case (0, let y): print("On y-axis at \(y)") case let (x, y): print("Point at (\(x), \(y))") } ``` ### Where Clause ```swift let point2 = (3, 3) switch point2 { case let (x, y) where x == y: print("On diagonal (x = y)") case let (x, y) where x == -y: print("On anti-diagonal") case let (x, y): print("Point at (\(x), \(y))") } ``` ### Switch with Tuples ```swift let person = (name: "Alice", age: 30) switch person { case ("Alice", 30): print("Perfect match!") case (_, 30): print("Name doesn't match") case ("Alice", _): print("Age doesn't match") default: print("No match") } ``` ## For Loops ### For-In with Range ```swift for i in 1...5 { print(i) // 1, 2, 3, 4, 5 } ``` ### For-In with Array ```swift let fruits = ["apple", "banana", "cherry"] for fruit in fruits { print(fruit) } ``` ### For-In with Dictionary ```swift let ages = ["Alice": 30, "Bob": 25, "Charlie": 35] for (name, age) in ages { print("\(name) is \(age) years old") } ``` ### For-In with Index ```swift let colors = ["red", "green", "blue"] for (index, color) in colors.enumerated() { print("\(index): \(color)") } ``` ### Ignoring Values ```swift let values = [1, 2, 3, 4, 5] let count = values.count for _ in 0.. 0 { print(countdown) countdown -= 1 } print("Liftoff!") ``` ### Repeat-While (Do-While) ```swift var number = 1 repeat { print(number) number += 1 } while number <= 5 // Runs at least once, even if condition is false var empty = false repeat { print("Runs once") } while empty ``` ## Control Transfer ### Break Exits loop early: ```swift let numbers = [1, 2, 3, 4, 5] for num in numbers { if num == 3 { break } print(num) // 1, 2 } ``` ### Continue Skips iteration: ```swift for num in 1...5 { if num == 3 { continue } print(num) // 1, 2, 4, 5 } ``` ### Fallthrough Continues to next case (unlike typical switch): ```swift let num = 2 switch num { case 1: print("One") fallthrough case 2: print("Two") fallthrough case 3: print("Three") default: print("Other") } // Output: Two, Three ``` ## Guard Statements ### Guard Basics ```swift func greet(name: String?) { guard let name = name else { print("No name provided") return } print("Hello, \(name)!") } greet(name: "Alice") // Hello, Alice! greet(name: nil) // No name provided ``` ### Guard with Multiple Conditions ```swift func process(age: Int?, name: String?) { guard let age = age, let name = name else { print("Missing required data") return } guard age >= 18 else { print("\(name) is too young") return } print("\(name) is \(age) and can proceed") } ``` ### Guard in Loops ```swift let data: [Int?] = [1, 2, nil, 4, 5] for item in data { guard let value = item else { continue } print(value * 2) // 2, 4, 8, 10 } ``` ## Early Return with Guard ```swift func configure(label: UILabel?) { guard let label = label else { return } label.text = "Hello" label.textColor = .black label.font = UIFont.systemFont(ofSize: 17) } ``` ## Labeled Statements ### Labeled Loops ```swift outerLoop: for i in 1...3 { innerLoop: for j in 1...3 { if i == 2 && j == 2 { break outerLoop // Breaks outer loop } print("i=\(i), j=\(j)") } } ``` ### Labeled Switch ```swift let matrix = [[1, 2], [3, 4]] var found = false searchLoop: for (i, row) in matrix.enumerated() { for (j, value) in row.enumerated() { if value == 3 { found = true print("Found at (\(i), \(j))") break searchLoop } } } ``` ## Summary - `if`, `if-else`, `if-else if-else` for conditions - `switch` with cases, ranges, value binding, where clauses - `for-in` loops with ranges, arrays, dictionaries - `while` and `repeat-while` loops - `break` exits loops/switches; `continue` skips iterations - `guard` for early exit with optional binding - `fallthrough` continues to next case - Labeled statements for nested control flow

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →