November 2016
Intermediate to advanced
480 pages
14h 42m
English
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 of the ...
Read now
Unlock full access