Reading — step 1 of 5
Learn
~1 min readControl Flow
if/else if/else works as expected. Conditions must be Bool — Swift doesn't have truthy/falsy values.
let score = 87
if score >= 90 {
print("A")
} else if score >= 80 {
print("B")
} else {
print("F")
}
switch is rich — it must be exhaustive and supports patterns:
let n = 7
switch n {
case 0:
print("zero")
case 1...10:
print("low")
case let x where x < 0:
print("negative: \(x)")
default:
print("large")
}
No fall-through by default. Cases can match ranges, tuples, types, with guards (where). Most switch statements look like pattern matches.
The ternary cond ? a : b works as in C/JavaScript.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…