Control flow
Braces are always required around bodies - there's no single-statement shorthand like C's if (x) doThing();.
if / elseif / else
Conditions must be a real boolean expression (see Truthiness) - a comparison, an equality check, or a call that returns true/false.
let score = 72;
if (score >= 90) {
output("A");
} elseif (score >= 80) {
output("B");
} elseif (score >= 70) {
output("C");
} else {
output("F");
}
// prints "C"
Note the keyword is elseif (one word), not else if.
while
let i = 0;
while (i < 3) {
output(i);
i++;
}
// 0, 1, 2
There is no do…while or C-style three-part for (init; cond; step) loop in Falx today - while and foreach are the only two loop constructs.
foreach
foreach (name: iterable) { ... } works over arrays, maps and strings - anything that implements iteration internally. The loop variable is declared fresh by the foreach itself; you don't write let.
foreach (n: [1, 2, 3]) {
output(n);
}
// 1, 2, 3
let m = { a: 1, b: 2 };
foreach (v: m) {
output(v); // iterates the map's VALUES: 1, 2
}
// use m.keys() alongside m[k] if you need the key too - see the Maps page
foreach (ch: "hi") {
output(ch); // "h", "i" - a string iterates one character at a time
}
foreach gives you each value, not a key/value pair. To act on both, iterate m.keys() and index back in: foreach (k: m.keys()) { let v = m[k]; ... } - the exact pattern the inventory management example uses.
break & continue
Both only work inside a while or foreach body - using either at top level, or outside a loop, is a parse-time error (break/continue outside a loop).
let i = 0;
while (i < 5) {
i++;
if (i == 2) { continue; }
if (i == 4) { break; }
output(i);
}
// 1, 3 - 2 is skipped, loop stops before printing 4