Conditional Statements

Run Settings
LanguageSwift
Language Version
Run Command
//if dan else adalah opsional //if, if-elseif-elseif dan if-elseif-else var temperatureInCelcius = 20 if temperatureInCelcius <= 25 { print("It's very cold. Consider wearing a scarf.") } // Mencetak "It's very cold. Consider wearing a scarf." var temperatureInCelcius = 28 if temperatureInCelcius <= 25 { print("It's very cold. Consider wearing a scarf.") } else { print("It's not that cold. Wear a t-shirt.") } // Prints "It's not that cold. Wear a t-shirt. var temperatureInCelcius = 35 if temperatureInCelcius <= 25 { print("It's very cold. Consider wearing a scarf.") } else if temperatureInCelcius >= 30 { print("It's really warm. Don't forget to wear sunscreen.") } else { print("It's not that cold. Wear a t-shirt.") } // Prints "It's really warm. Don't forget to wear sunscreen. var temperatureInCelcius = 30 if temperatureInCelcius <= 25 { print("It's very cold. Consider wearing a scarf.") } else if temperatureInCelcius >= 40 { print("It's really warm. Don't forget to wear sunscreen.") } //Switch,case dan default case let someCharacter: Character = "z" switch someCharacter { case "a": print("The first letter of the alphabet") case "z": print("The last letter of the alphabet") default: print("Some other character") } // Prints "The last letter of the alphabet" //No Implicit Fallthrough dengan menambahkan break agar switch aman dan mudah menghindari masalah saat di compile let anotherCharacter: Character = "a" switch anotherCharacter { case "a": // Invalid, the case has an empty body case "A": print("The letter A") default: print("Not the letter A") } // This will report a compile-time error. let anotherCharacter: Character = "a" switch anotherCharacter { case "a", "A": print("The letter A") default: print("Not the letter A") } // Prints "The letter A" //Interval Matching let approximateCount = 62 let countedThings = "moons orbiting Saturn" let naturalCount: String switch approximateCount { case 0: naturalCount = "no" case 1..<5: naturalCount = "a few" case 5..<12: naturalCount = "several" case 12..<100: naturalCount = "dozens of" case 100..<1000: naturalCount = "hundreds of" default: naturalCount = "many" } print("There are \(naturalCount) \(countedThings).") // Prints "There are dozens of moons orbiting Saturn. //Tuples let somePoint = (1, 1) switch somePoint { case (0, 0): print("\(somePoint) is at the origin") case (_, 0): print("\(somePoint) is on the x-axis") case (0, _): print("\(somePoint) is on the y-axis") case (-2...2, -2...2): print("\(somePoint) is inside the box") default: print("\(somePoint) is outside of the box") } // Prints "(1, 1) is inside the box" //Value Bindings let anotherPoint = (2, 0) switch anotherPoint { case (let x, 0): print("on the x-axis with an x value of \(x)") case (0, let y): print("on the y-axis with a y value of \(y)") case let (x, y): print("somewhere else at (\(x), \(y))") } // Prints "on the x-axis with an x value of 2" //Where let yetAnotherPoint = (1, -1) switch yetAnotherPoint { case let (x, y) where x == y: print("(\(x), \(y)) is on the line x == y") case let (x, y) where x == -y: print("(\(x), \(y)) is on the line x == -y") case let (x, y): print("(\(x), \(y)) is just some arbitrary point") } // Prints "(1, -1) is on the line x == -y" //Compound Cases let someCharacter: Character = "e" switch someCharacter { case "a", "e", "i", "o", "u": print("\(someCharacter) is a vowel") case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z": print("\(someCharacter) is a consonant") default: print("\(someCharacter) is not a vowel or a consonant") } // Prints "e is a vowel" let stillAnotherPoint = (9, 0) switch stillAnotherPoint { case (let distance, 0), (0, let distance): print("On an axis, \(distance) from the origin") default: print("Not on an axis") } // Prints "On an axis, 9 from the origin"
Editor Settings
Theme
Key bindings
Full width
Lines