Skip to content

Control flow

if / else if / else

munos
if score > 100 {
    print("high")
} else if score > 50 {
    print("medium")
} else {
    print("low")
}

The condition must be a bool — there is no truthiness coercion, so write if n != 0 { … }, not if n { … }.

while

munos
while condition {
    // runs while condition is true
}

for

C-style: for (init; condition; step) { … }. The init clause may declare a loop-scoped variable with var.

munos
for (var i = 0; i < len(players); i += 1) {
    draw_image(sprite, pal, playerX[i], playerY[i])
}

The three clauses are separated by ; — the one place a semicolon is mandatory in Munos.

break and continue

  • break exits the innermost enclosing loop.
  • continue skips to the next iteration.
munos
for (var i = 0; i < max_clients(); i += 1) {
    if !occupied[i] { continue }
    // ...process occupied slot i...
}

return

Exits the current function, optionally with a value (see Functions).

munos
function clamp_byte(n: i32): i32 {
    if n < 0 { return 0 }
    if n > 255 { return 255 }
    return n
}

Part of the MultiNostalgia project.