|
| 1 | +// Simple if statement |
| 2 | +let temperature = 25 |
| 3 | +if temperature > 30 { |
| 4 | + print("It's hot outside!") |
| 5 | +} |
| 6 | + |
| 7 | +// If-else statement |
| 8 | +let score = 85 |
| 9 | +if score >= 90 { |
| 10 | + print("Excellent!") |
| 11 | +} else if score >= 80 { |
| 12 | + print("Good job!") |
| 13 | +} else if score >= 70 { |
| 14 | + print("Passing") |
| 15 | +} else { |
| 16 | + print("Needs improvement") |
| 17 | +} |
| 18 | + |
| 19 | +// MARK: - Optional Binding with If |
| 20 | + |
| 21 | +// Using if let for optional binding |
| 22 | +let possibleNumber = "123" |
| 23 | +if let actualNumber = Int(possibleNumber) { |
| 24 | + print("The string \"\(possibleNumber)\" has an integer value of \(actualNumber)") |
| 25 | +} else { |
| 26 | + print("The string \"\(possibleNumber)\" could not be converted to an integer") |
| 27 | +} |
| 28 | + |
| 29 | +// Multiple optional bindings |
| 30 | +let possibleName: String? = "John" |
| 31 | +let possibleAge: Int? = 30 |
| 32 | +if let name = possibleName, let age = possibleAge { |
| 33 | + print("\(name) is \(age) years old") |
| 34 | +} |
| 35 | + |
| 36 | +// MARK: - Guard Statements |
| 37 | +func greet(person: [String: String]) { |
| 38 | + guard let name = person["name"] else { |
| 39 | + print("No name provided") |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + guard let age = person["age"], let ageInt = Int(age) else { |
| 44 | + print("Invalid age provided") |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + print("Hello \(name), you are \(ageInt) years old") |
| 49 | +} |
| 50 | + |
| 51 | +// MARK: - Switch Statements |
| 52 | +// Switch with range matching |
| 53 | +let approximateCount = 62 |
| 54 | +let countedThings = "moons orbiting Saturn" |
| 55 | +let naturalCount: String |
| 56 | +switch approximateCount { |
| 57 | +case 0: |
| 58 | + naturalCount = "no" |
| 59 | +case 1..<5: |
| 60 | + naturalCount = "a few" |
| 61 | +case 5..<12: |
| 62 | + naturalCount = "several" |
| 63 | +case 12..<100: |
| 64 | + naturalCount = "dozens of" |
| 65 | +case 100..<1000: |
| 66 | + naturalCount = "hundreds of" |
| 67 | +default: |
| 68 | + naturalCount = "many" |
| 69 | +} |
| 70 | +print("There are \(naturalCount) \(countedThings).") |
| 71 | + |
| 72 | +// Switch with tuple matching |
| 73 | +let somePoint = (1, 1) |
| 74 | +switch somePoint { |
| 75 | +case (0, 0): |
| 76 | + print("(0, 0) is at the origin") |
| 77 | +case (_, 0): |
| 78 | + print("(\(somePoint.0), 0) is on the x-axis") |
| 79 | +case (0, _): |
| 80 | + print("(0, \(somePoint.1)) is on the y-axis") |
| 81 | +case (-2...2, -2...2): |
| 82 | + print("(\(somePoint.0), \(somePoint.1)) is inside the box") |
| 83 | +default: |
| 84 | + print("(\(somePoint.0), \(somePoint.1)) is outside of the box") |
| 85 | +} |
| 86 | + |
| 87 | +// Switch with value binding |
| 88 | +let anotherPoint = (2, 0) |
| 89 | +switch anotherPoint { |
| 90 | +case (let x, 0): |
| 91 | + print("on the x-axis with an x value of \(x)") |
| 92 | +case (0, let y): |
| 93 | + print("on the y-axis with a y value of \(y)") |
| 94 | +case let (x, y): |
| 95 | + print("somewhere else at (\(x), \(y))") |
| 96 | +} |
| 97 | + |
| 98 | +// MARK: - Fallthrough |
| 99 | +// Using fallthrough in switch |
| 100 | +let integerToDescribe = 5 |
| 101 | +var description = "The number \(integerToDescribe) is" |
| 102 | +switch integerToDescribe { |
| 103 | +case 2, 3, 5, 7, 11, 13, 17, 19: |
| 104 | + description += " a prime number, and also" |
| 105 | + fallthrough |
| 106 | +default: |
| 107 | + description += " an integer." |
| 108 | +} |
| 109 | +print(description) |
| 110 | + |
| 111 | +// MARK: - Labeled Statements |
| 112 | +// Using labeled statements with break |
| 113 | +let finalSquare = 25 |
| 114 | +var board = [Int](repeating: 0, count: finalSquare + 1) |
| 115 | +board[03] = 8 |
| 116 | +board[06] = 11 |
| 117 | +board[09] = 9 |
| 118 | +board[10] = 2 |
| 119 | +board[14] = -10 |
| 120 | +board[19] = -11 |
| 121 | +board[22] = -2 |
| 122 | +board[24] = -8 |
| 123 | + |
| 124 | +var square = 0 |
| 125 | +var diceRoll = 0 |
| 126 | +while square != finalSquare { |
| 127 | + diceRoll += 1 |
| 128 | + if diceRoll == 7 { diceRoll = 1 } |
| 129 | + switch square + diceRoll { |
| 130 | + case finalSquare: |
| 131 | + break |
| 132 | + case let newSquare where newSquare > finalSquare: |
| 133 | + continue |
| 134 | + default: |
| 135 | + square += diceRoll |
| 136 | + square += board[square] |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// MARK: - For Loops |
| 141 | +// For-in loop with enumerated() to get index and value |
| 142 | +print("\n=== For-in with Enumerated ===") |
| 143 | +for (index, name) in names.enumerated() { |
| 144 | + print("\(index): \(name)") |
| 145 | +} |
| 146 | + |
| 147 | +// For-in loop with where clause |
| 148 | +print("\n=== For-in with Where Clause ===") |
| 149 | +let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 150 | +for number in numbers where number % 2 == 0 { |
| 151 | + print("Even number: \(number)") |
| 152 | +} |
| 153 | + |
| 154 | + |
0 commit comments