Control flow
Control flow
Choosing and repeating: if, unless, while, for, loop, break and continue, guard, defer and tail calls, one page each.
| Feature | What it does | Page |
|---|---|---|
if, else, ?: | Choose between branches; if is also an expression. | if and else |
unless | if with the condition turned around. | unless |
while | Repeat while a condition holds. | while |
for | Walk a range, a list, a map, a string or any iterator. | for |
a..b | Ranges, step, and what they can and cannot do. | Ranges |
loop | Repeat until break; the only loop that gives a value. | loop |
break, continue, labels | Leave or skip, including outer loops. | break, continue and labels |
guard | Check a condition up front and leave early. | guard |
defer | Run code when the scope ends. | defer |
become | A guaranteed tail call. | Tail calls |
Pattern matching with switch and is has its own chapter: Pattern matching.
Every condition must be a bool. There is no truthy or falsy value: if n { ... } with an int is an error.