Control Structures

The following control structures can be used in fi:

  • if/else

  • throw

  • assert

We are currently working on a few additional control structures, including:

  • foreach

  • loop

If/else

If statements allow you to branch your logic in multiple ways based on the resolution of an expression. If the expression resolves as true, the code within the curly braces is executed.

if (SENDER == address "tz1NhSA8NV4W3e5ws37u1xzjkgxDCpijyh7m" || OWNER == SENDER) {
}

else

A second block of code can be placed within curly braces after the initial block, prepended with the else control. You can also use else if to define another condition expression to evaluate.

if (SENDER == address "tz1NhSA8NV4W3e5ws37u1xzjkgxDCpijyh7m") {
return.isOwner = bool True;
} else if (nat 2 == add(nat 1, to_nat(sub(nat 5, int 2, nat 2)))){
} else {
}

Throw

This allows the developer to exit execution of the script - an optional value can be provided as an argument, which will be returned as an error.

if (SENDER != SOURCE){
throw(string "Error with sender");
}

Assert

The assert allows the developer to exit execution if a condition returns false - essentially the combination of an if and a throw. The first argument must be the condition to evaluate, and the optional second argument can be the value to be returned as an error. The previous example can also be written as:

assert(SENDER == SOURCE, string "Error with sender");