else
The else keyword introduces the alternative branch of an if statement (or expression). It runs when the preceding if/elif conditions are all falsy.
Syntax & Example
if x > 0 {
print("positive");
} else {
print("zero or negative");
}
let abs = if x >= 0 { x } else { -x };
Use Cases
- Providing a fallback branch when no
if/elifcondition matches. - Pairing with
ifexpressions to guarantee a value in all cases.