Skip to main content

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/elif condition matches.
  • Pairing with if expressions to guarantee a value in all cases.