Booleans and Conditionals
The type of a boolean value is
bool. Here is how to define a boolean
In LIGO, only values of the same type can be compared. Moreover, not
all values of the same type can be compared, only those with
comparable types, which is a concept lifted from
Michelson. Comparable types include, for instance,
address, etc. As an example of
non-comparable types: maps, sets or lists are not comparable: if you
wish to compare them, you will have to write your own comparison
Note: when running in test mode (this is, in the testing framework), for developer convinence, more types are made comparable. Maps, sets and lists will be made comparable in case its elements are comparable.
tezvalues is especially useful when dealing with an amount sent in a transaction.
Conditional logic enables forking the control flow depending on the state.
You can run the
compare function defined above using the LIGO compiler
JsLIGO also supports branching of control flow via the switch statement.
The switch statement takes an expression and tries to find a
case which matches the switch expression,
If a matching
case is found, the statements of the matching case are executed untill a
break is found the control falls through to the next
default. If no matching case is found
the statements of the
default case are executed.
A few gotcha's about the switch statement
- A switch should have at-least one
- If a
defaultcase is provided, It should be the last case.
break's are not supported i.e.
- In case of nested
switchstatements, the inner
switchshould not contain a
You can run the
quarter function defined above using the LIGO compiler
which can also be nested: