func getMonitorSize() -> Int {
return (14...27).randomElement()!
}
var str: String
let inchSize = getMonitorSize()
switch inchSize {
case 14,15:
str = "too small"
case 16,17,18:
str = "good for the past decade"
case 19...23:
str = "for office work"
case 24..<28:
str = "great choice"
default:
str = ""
}
print("Monitor \(inchSize)\" is \(str)")
| A switch statement considers a value and compares it against several possible matching patterns. It then executes an appropriate block of code, based on the first pattern that matches successfully. A switch statement provides an alternative to the if statement for responding to multiple potential states. In its simplest form, a switch statement compares a value against one or more values of the same type. Every switch statement consists of multiple possible cases, each of which begins with the case keyword. Values in switch cases can be checked for their inclusion in an interval. |