if / else
Try it yourself →Branching based on conditions.
Syntax
if condition { ... } else { ... }Description
Conditions are written without parentheses; the { } block is mandatory. If true, the first block runs, otherwise the else runs. Intermediate conditions use else if.
Example
let age = 20
if age >= 18 {
print("Adult")
} else {
print("Child")
}Expected output
Adult
When to use?
Decision making, input control.