November 2016
Intermediate to advanced
480 pages
14h 42m
English
The ternary operator is very similar to an if/else statement, but has the more concise syntax a ? b : c.
In English, the ternary operator reads something like, “If a is true, then do b.
Otherwise, do c.”
Let’s rewrite the town population check that used if/else using the ternary operator instead.
Listing 3.3 Using the ternary operator
...if population < 10000 {message = "\(population) is a small town!"} else {message = "\(population) is pretty big!"}message = population < 10000 ? "\(population) is a small town!" : "\(population) is pretty big!" ...
The ternary operator can be a source of controversy: Some programmers love it; some programmers loathe it. We come down somewhere in the middle. This particular ...
Read now
Unlock full access