Statements


Statements are the base of imperative programming. They are fragments of the program executed in sequence. While statements cannot be used on their own, they constitute the body of every function. Statements cannot directly return a value (see Expressions [nils link here]), but they can alter the control flow or produce side effects.

The following statements are implemented by [name]:

Variable definition


The syntax for the definition of variables has already been mentioned in earlier pages.

Expression statement


Expression statements are statements which evaluate an expression, ignoring the result.

n = 5

An expression (assignment) statement. Ignores the produced result of 5.

Block statement


A block statement is a statement which consists of a sequence of statements delimited by curly brackets ({}). Often used as function body or for bodies of other statements.

Conditional statements


If statement

An if statement evaluates a condition and executes the code in the "then" branch if the value is truey. If an "else" branch is present, that is executed instead.

if (condition) then statement

if (condition) then statement else else statement

Match statement [match?]

A match statement takes in a value and compares it to a series of user-set values, and selects the branch to run accordingly. Unlike C-like switch statements, execution does not fall to the next statement if a "break" is missing. Like C-like switches, though, the match statement only accepts integer values for branches. There is no limit to branches, and a default fall-through branch is optional.

match (value) ( value => statement fallback statement

Loops


Loop statement

A loop statement is a statement that runs indefinitely.

loop statement

While Loop

A while loop is a type of loop that executes the body statement as long as the condition is met.

while (condition) statement

It also comes in the do-while variant, which runs the body at least once.

do (condition) while statement;

For Loop

A for loop is similar to a while loop but, along with a condition, it also has an initialization statement and an increment statement that runs for each iteration.

for (init; condition; increment) statement

Control flow

Keep in mind, multiple return/break statements might take less CPU cycles but often make the code less readable. Every problem that can be solved with a return/break statement can also be solved with conditional, linear control flow.


Return statement

A function with a return type different from v requires at least one return statement. If it is missing, the compiler will throw a warning and automatically put one at the end of the function, returning zero.

return expression;

Break statement

Break and continue statements are used to alter the flow of execution in a loop: break ends the loop, while continue skips the rest of the code and directly goes to the next iteration of the loop. Note that, in for loops, continue does NOT skip the increment.