Control flow

if/elif/else, while, a C-style for, break and continue. Any non-zero value is true, and a str is true when it is not empty.

if / elif / else

#include <libstd/stdio>

def describe(n: int) -> None:
    if n > 5:
        print("big\n")
    elif n > 2:
        print("medium\n")
    else:
        print("small\n")

describe(3)
medium

The condition is any expression; parentheses are optional (while (z < 20): is accepted). Conditions are evaluated with plain compare-and-jump, so a comparison used in an if costs nothing extra.

A bare value used as a condition is true when it is not zero: a number other than 0 or 0.0, a pointer that is not null (an object, an array, a ref, ptr T). A str is true when it is not empty, as in Python: if s: means if s != "":, and a null str counts as empty instead of crashing. not, and and or follow the same rule.

#include <libstd/stdio>

def greet(name: str) -> None:
    if name:
        print("hello ", name, "\n")
    else:
        print("hello, whoever you are\n")

greet("Ada")
greet("")
hello Ada
hello, whoever you are

while

z = 5
while z > 0:
    z--

while 1:                 // an infinite loop, left with break
    fd_client = accept(fd_server, 0)
    if fd_client < 0:
        continue
    handle(fd_client)

for

The for loop has the three C parts, separated by semicolons: initialisation, condition, step. The step can be an increment, a decrement, or any assignment (including a function call), and several statements can be separated by commas.

def somma(num: int) -> int:
    tot = 0
    for i = 0; i < num; i++:
        tot = tot + i
    return tot

for k = 10; k > 0; k = k - 3:
    print(k, " ")               // 10 7 4 1

for row = reader_next_row(r); r.eof == 0; row = reader_next_row(r):
    handle(row)

There is no for x in collection: iterate a List with an index and list_get, a dict with a DictIterator (see Lists & dicts).

break and continue

i = 0
while 1:
    i++
    if i % 2 == 0:
        continue                 // next iteration
    if i > 7:
        break                    // leave the loop
    print(i, " ")                // 1 3 5 7

Early return inside a loop

A return can appear anywhere, including inside nested loops: the compiler closes the arena scopes of the loops that are still open before returning (see The arena). Inside an inline def the precompiler rewrites an early return placed as the last statement of a loop body into a flag checked by the loop condition; other positions simply keep the function as a real call.

inline def first_multiple_of_3(n: int) -> int:
    i = 0
    while i < n:
        i = i + 1
        if i % 3 == 0:
            return i
    return -1

Loops and memory

Every iteration of a while or for is a scope: strings and objects created inside it and not stored anywhere longer-lived are released at the end of the iteration, so a loop that builds a temporary string on every pass does not grow the memory of the program. Loops that provably never allocate get no scope at all (see scope elision).

i = 0
while i < 1000000:
    line = "row " + itoa(i) + "\n"   // freed at the end of each iteration
    if i % 100000 == 0:
        print(line)
    i++

exit

exit(code) terminates the process immediately with the given exit code, from any depth.