Control flow

Statements live inside a block (exec, fn, route, command, before), one statement per line, each ending in ;. // starts a comment. Blank lines are skipped, and } may share a line with else {.

if and else

The condition must be a boolean. There is no truthiness: if (1) is E0230, and the fix is to write the comparison you meant.

check-1.md
---
midad: "0.1"
module: truthy
---

```exec
if (1) {
  print("yes");
}
```
midad check
× E0230: type mismatch: expected boolean, found number

`else if` chains, and a whole `if` can sit on one line when it is short:

````midad run branches.md
---
midad: "0.1"
module: branches
---

# branches

```exec
const n = 3;
if (n > 5) {
  print("five");
} else if (n > 2) {
  print("two");
} else {
  print("none");
}

if (n > 2) { print("big"); } else { print("small"); }

```output
two
big
```

## `while`, `break`, `continue`

`while (condition) { ... }` runs while the condition is true. `break` leaves
the innermost loop and `continue` goes back to its test. Both are statements,
not expressions, and both apply to the innermost `while`.

````midad run loops.md
---
midad: "0.1"
module: loops
---

# loops

```exec
let i = 0;
let total = 0;
while (i < 5) {
  i = i + 1;
  if (i == 3) { continue; }
  if (i == 5) { break; }
  total = total + i;
}
print("total " + str(total));
```
output
total 7

The interpreter gives every program a step budget, so a runaway while stops with an error instead of hanging the process. An HTTP request gets 50 million steps.

for over a list

for name in list { ... } runs the body once per element, in order. The loop variable is any, because a list holds any.

foreach.md
---
midad: "0.1"
module: foreach
---

# foreach

```exec
let total = 0;
for price in [120, 45, 300] {
  total = total + int(price);
}
print(str(total));
print(str(len([1, 2, 3])));
```
output
465
3

for is spelled out at parse time as a while over an index, so every backend runs it the same way. There is no for over a range and no for over a map; for a map, loop over keys(m).

par

par name in A..B with accumulator { ... } runs the iterations independently and sums what each one adds to the accumulator. The C backend runs it on threads; the interpreter runs it serially and gets the same answer.

par.md
---
midad: "0.1"
module: par
---

# par

```exec
let sum = 0;
par k in 0..100 with sum {
  sum = sum + k;
}
print("par " + str(sum));
```
output
par 4950

This is the only parallel construct in the language: no threads you create, no channels, no locks, no async/await. On one core it is slower than the serial loop, and no speedup has been measured on more than one core. The limits page records both facts.

Operators

operator on answers
+ two numbers, two strings, two floats the sum or the join
- * two numbers or two floats the result
/ two numbers or two floats a float, always
% two numbers the remainder
== != two values of the same type a boolean
< <= > >= two numbers, two floats, two strings a boolean
and && two booleans a boolean, short-circuiting
or || two booleans a boolean, short-circuiting
not ! a boolean a boolean
- (prefix) a number or float the negation

and/&&, or/|| and not/! are the same operators spelled two ways. Comparisons do not chain the way they read: 1 < x < 5 parses as (1 < x) < 5, and the checker refuses it. Write 1 < x && x < 5.

The traps worth knowing before they cost you an hour:

  • / is float division. 7 / 2 is 3.5, not 3. Integer division is idiv(a, b), which floors: idiv(-7, 2) is -4. The checker warns (W008) when it sees / where integer division looks intended.
  • // is a comment, not integer division.
  • Numbers and floats never mix. 1 == 1.0 is a compile error; write float(1) == 1.0 or int(1.0) == 1.
  • "a" + 1 is a compile error (E0230). Write "a" + str(1).

The generated operator reference is the same list read out of the binary.

Statements

statement meaning
let x: T = e; a mutable binding
const x: T = e; an immutable binding; reassigning one is E0234
x = e; assignment to a mutable binding
return e; / return; leave the function
if / while / for / par the loops above
break; / continue; inside a while
f(a, b); a call whose answer is not used

A call whose answer is not used is fine. Three builtins change their first argument in place and answer nothing, so they are written as statements of their own: push(xs, x);, set_at(xs, i, v); and sort(xs);. Using their result is E0260, because there is no result to use. remove_at(xs, i) changes the list and answers the element it removed; put(m, k, v) and del(m, k) change the map and answer it, so assigning the result is allowed but not needed. mutation and copying has the table.

Nesting limits

An expression nested more than 256 levels and blocks nested more than 64 deep are a diagnostic (E0100, E0102) rather than a stack overflow. That is a limit you will only meet in generated code.

Next: functions.

On this page