Nested ifs
You can nest if
statements for scenarios with more than two possibilities.
You do this by writing an if/else
statement inside the curly braces of another if/else
statement.
To see this, nest an if/else
statement within the else
block of your existing if/else
statement.
Listing 3.5 Nesting conditionals
import Cocoa var population: Int = 5422 var message: String var hasPostOffice: Bool = true if population < 10000 { message = "\(population) is a small town!" } else { if population >= 10000 && population < 50000 { message = "\(population) is a medium town!" } else { message = "\(population) is pretty big!" } } print(message) if !hasPostOffice { print("Where do we buy stamps?") }
Your nested if
clause makes use ...
Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.