Statements

Note

This page is under construction.

Several statements involve boolean conversion; for more details, see Boolean conversion.

Code block

A code block is a valid statement.

Variable assignment

A variable assignment consists of a left-hand side (LHS) expression (which must be assignable), followed by an assignment operator, and then a right-hand side (RHS) expression to be evaluated and assigned to the LHS.

The assignment operator must be either =, +=, -=, *=, /=, %=, &=, |=, ^=, **=, <<=, >>=, or >>>=. = assigns the value of the RHS to the LHS directly and discards the previous value of the LHS expression. All the other operators first evaluate the LHS and then perform some operation using the values of the LHS and RHS expressions. For example, X += Y (where X and Y are any arbitrary expressions) is equivalent to X = X + Y.

Examples:

Branching statements

If statement

An if statement consists of the keyword if, followed by a condition expression, and then a code block. The condition expression must be able to be converted to a boolean. An if statement evaluates the condition expression and if the result is truthy it executes the statements inside the code block.

Example:

Else-if statement

An else-if statement consists of the keyword else, followed by an if statement. The condition expression must be able to be converted to a boolean. An else-if statement must immediately follow an if statement or another else-if statement. If all of the condition expressions of the immediately preceding if statements and else-if statements were falsey, the else-if statement will execute evaluate its own expression, and if it is truthy it executes the statements inside the code block.

Examples:

Else statement

An else statement consists of the keyword else, followed by a code block. An else statement must immediately follow an if statement or else-if statement. If all of the condition expressions of the immediately preceding if statements and else-if statements were falsey, the else statement will the statements inside the code block.

Examples:

Loops

For loop

An if statement consists of the keyword for, followed by a left-hand side (LHS) expression (which must be assignable), the keyword in, a right-hand side (RHS) expression, and then a code block. The RHS must evaluate to an iterable type (TODO: link this to something, or explain it right here) and the LHS must evaluate to the iteration type of the RHS (TODO: explain this better). The statements inside the code block are executed for each iteration value (TODO: explain what this means, or use a better term) of the RHS.

Example:

Break statement

A break statement consists of the keyword break. It causes execution to jump to immediately after the innermost loop being executed. After a break statement, the loop will not iterate further.

Example:

Continue statement

A continue statement consists of the keyword continue. It causes execution to jump to the end of the current iteration of the innermost loop being executed. After a continue statement, the loop will continue iterating if the exit condition is not yet met.

Example:

Debugging

Error statement

An error statement consists of the keyword error, optionally followed by a String specifying a custom error message. An error statement causes an error, which aborts the simulation.

Examples:

Assert statement

An assert statement consists of the keyword assert, followed by an expression, and then an optional comma and String specifying a custom error message. The expression must be able to be converted to a boolean. An assert statement evaluates the expression and if the result is falsey it causes an error, which aborts the simulation.

Examples:

Returning statements

Become statement

A become statement consists of the keyword become, followed by an expression. The expression must evaluate to a Cell. A become statement evaluates the expression and transitions the current cell to the value of that expression. Become statements can only be used in transition functions (see @transition).

TODO: cell filter in become statement? also provide example

Example:

Remain statement

A remain statement consists of the keyword remain. It is equivalent to become this. NOTE: this may change in the future. what if this is modified?

Example:

Return statement

A return statement consists of the keyword return, followed by an expression. The expression must evaluate to a value of the same type as the return type of the function. A return statement evaluates the expression and transitions the current cell to the value of that expression. Return statements can only be used in helper functions (see @function).

Example: