Booleans and Conditionals
Booleans
The type of a boolean value is bool. Here is how to define a boolean
value:
Common operations:
Comparing Values
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, int, nat, bytes
string, tez, timestamp, 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
function.
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.
Comparing Strings
Comparing numbers
Comparing bytes
To check if the following operators have the expected result use
ligo compile expression jsligo "a OP b"
Usage:
Comparing tez
💡 Comparing
tezvalues is especially useful when dealing with an amount sent in a transaction.
Conditionals
Conditional logic enables forking the control flow depending on the state.
You can run the compare function defined above using the LIGO compiler
like this:
Switch Statement
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; statement.
If no break is found the control falls through to the next case or 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
caseordefault.- If a
defaultcase is provided, It should be the last case.- Conditional
break's are not supported i.e.breakinside aif-then-else.- In case of nested
switchstatements, the innerswitchshould not contain areturn.
You can run the quarter function defined above using the LIGO compiler
like this:
Ternary conditional expression
JsLIGO also supports JavaScript's ternary expression:
which can also be nested: