To be answered
Test your understanding of Swift Control flow by taking this Quiz
func calculate() -> Int { var result = 0 for i in 1...5 { if i % 2 == 0 { result += i } else { result -= i } } return result }
func printNames() { let names = ["John", "Amy", "Emily", "Michael"] for name in names { if name == "Amy" { continue } print(name) if name == "Emily" { break } } }
func calc() { defer { print("Step 3") } print("Step 1") defer { print("Step 4") } print("Step 2") }
func calc() -> Int { var x = 10 repeat { x += 5 } while x < 30 return x }
func checkOS() { let condition = false if #available(iOS 14, *) { print("Running on iOS 14 or later") } else { print("Running on earlier iOS version") } }
func printChars() -> String { var result = "" for i in 1...3 { switch i { case 1, 3: result += "A" case 2: result += "B" default: result += "C" } } return result }
func example() { var x = 10 defer { x += 5 } if x > 5 { print(x) return } print(x - 5) }
Subscribe to our newsletter