Statements
Statements largely work like in C, but with some additions.
Expression blocks
Expression blocks (delimited using {| |}
) are compound statements that opens their own function scope.
Jumps cannot be done into or out of a function block, and return
exits the block, rather than the function as a whole.
The function below prints World!
Expression blocks may also return values:
Labelled break and continue
Labelled break
and continue
lets you break out of an outer scope. Labels can be put on if
,
switch
, while
and do
statements.
Do-without-while
Do-while statements can skip the ending while
. In that case it acts as if the while
was while(0)
:
Nextcase and labelled nextcase
The nextcase
statement is used in switch
and if-catch
to jump to the next statement:
It’s also possible to use nextcase
with an expression, to jump to an arbitrary case:
Which can be used as structured goto
when creating state machines.
Switch cases with runtime evaluation
It’s possible to use switch
as an enhanced if-else chain:
The above would be equivalent to writing:
Note that because of this, the first match is always picked. Consider:
Because of the evaluation order, only foo()
will be invoked for x > 0, even when x is greater than 2.
It’s also possible to omit the conditional after switch
. In that case it is implicitly assumed to be same as
writing (true)