Control Flow¶
if / elif / else¶
elifandelseare optional.- Braces
{ }are always required.
Ternary operator¶
A compact inline conditional that returns a value:
Ternaries can be chained:
while¶
for — range loop¶
Iterates from start to end inclusive:
for — for-each loop¶
Iterates over lists, string characters, or dictionary keys:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits {
write(fruit)
}
for ch in "hello" {
write(ch)
}
for key in {"a": 1, "b": 2} {
write(key)
}
switch¶
Compares a value against multiple cases. Each case can match multiple values.
- Cases are checked in order; only the first match runs.
elseis optional and acts as the fallback.- The subject is compared with
==.
match¶
Like switch but is an expression — it returns a value.
_is the wildcard — matches anything and must be last.- Each arm is
pattern => expression. - Multiple patterns per arm:
2, 3 => "two or three".