Loop expressions

"Loop" expressions

The most flexible type of loop is loop.
Like with if, the body goes in an indented block.
The body should be an expression ending in break or continue.

main void() x mut nat = 0 loop if x < 10 info log "{x}" x +:= 1 continue else break

Loops as expressions

The above example shows a void loop. Loops can have any type. The value of the loop is the value passed to break. (This value defaults to () if not written explicitly.)
This avoids using an unnecessarily mutable variable for the loop result.

main void() n nat = 50 # Find the smallest square number greater than 'n' x mut nat = 0 result nat = loop square = x * x if square < n x +:= 1 continue else break square info log "{result}"

"While" expressions

while is as a shorthand for a loop that breaks if the condition is false, else runs the body and continues.
So the first example could be rewritten as:

main void() x mut nat = 0 while x < 10 info log "{x}" x +:= 1

"Until" expressions

until is the same as while, but the condition is negated.

main void() x mut nat = 0 until x >= 10 info log "{x}" x +:= 1

Tail recursion

When a function ends by calling itself, crow replaces the function body with a loop for efficiency.

main void() 0 f f void(x nat) if x < 10 info log "{x}" # This is like a 'continue' x + 1 f else # This is like a 'break' ()

If you use tail recursion, you don't technically need loop, and vice versa.
Choose whichever option suits your style.