Loops

"while" loops

A while expression runs as long as the condition is true.
The value of a while expression is always void.

main void() x mut = 10 while x != 0 x -:= 1 log x

The condition of a while loop can be an option.

main void() xs nat mut[] = 1, 2, 3 while x = pop xs log x

Condition-bodied "while" loops

The condition and body can be combined. This will repeatedly evaluate the body as long as it returns true.
Unlike the above example, this has an additional log of an empty option.

main void() xs nat mut[] = 1, 2, 3 while x = pop xs log x !!x

"until" loops

An until expression is like a while expression with the condition negated.

main void() x mut nat = 10 until x == 0 log x x -:= 1

As with a while loop, the condition can be an option.
Since the condition is negated, the value is avaluable after the loop.

main void() x mut nat = 11 until result = tenth x # 'result' is not in scope here log "x is", x x +:= 1 log "result is", result tenth nat?(a nat) if is-multiple-of a, 10 a / 10

Condition-bodied "until" loops

As with a while loop, the condition and body can be combined.

main void() x mut = 10 until log x x -:= 1 x == 0

The body can also return an option.
In that case, the value of the until expression is the first non-empty option returned by the body.
(It is unwrapped to a non-optional type, meaning nat instead of nat? below.)
This is the only kind of loop to not be void.

main void() x mut = 11 result = until log "x is", x x +:= 1 tenth x log "result is", result tenth nat?(a nat) if is-multiple-of a, 10 a / 10