When we want to set a variable for a specific condition, this what we normally do:
let temperatureInCelsius = 25 let weatherAdvice: String if temperatureInCelsius <= 0 { weatherAdvice = "It's very cold. Consider wearing a scarf." } else if temperatureInCelsius >= 30 { weatherAdvice = "It's really warm. Don't forget to wear sunscreen." } else { weatherAdvice = "It's not that cold. Wear a T-shirt." } print(weatherAdvice) // Prints "It's not that cold. Wear a T-shirt."
We can make it more cleaner using if expression
:
let temperatureInCelsius = 25 let weatherAdvice = if temperatureInCelsius <= 0 { "It's very cold. Consider wearing a scarf." } else if temperatureInCelsius >= 30 { "It's really warm. Don't forget to wear sunscreen." } else { "It's not that cold. Wear a T-shirt." } print(weatherAdvice) // Prints "It's not that cold. Wear a T-shirt."
Learn more:
Top comments (0)