Control flow
Same semantics as C, written with indentation. There are no braces, no then and no block colons.
Today
f sum(a: *i32, n: i32) i32
total := 0
for i := 0; i < n; i++
if a[i] < 0
^
elif a[i] == 0
!
else
total += a[i]
while total > 10
total--
< total
f main
v: i32[] = {4, -1, 9, 0, 7}
sum(v, 5) != 10int sum(int *a, int n) {
int total = 0;
for (int i = 0; i < n; i++) {
if (a[i] < 0) {
continue;
} else if (a[i] == 0) {
break;
} else {
total += a[i];
}
}
while (total > 10) {
total--;
}
return total;
}
int main(void) {
int v[] = {4, -1, 9, 0, 7};
return sum(v, 5) != 10;
}| Word | C |
|---|---|
if / elif / else | if / else if / else |
while cond | while |
for init; cond; step | for |
! | break |
^ | continue |
< value | return value |
Indentation is two spaces. A body must be deeper than its header and siblings must align; the compiler reports inconsistent indentation as an error.
Target: if as an expression
In the new core if also produces a value, so a conditional initializer needs no temporary or ternary.
f pick(a: i32) i32
r := if a == 1 2 else 3
rRelated
- Functions for results and early returns.
- The guide lesson covers the syntax table in full.