Booleans and Conditionals
#
BooleansThe type of a boolean value is bool
. Here is how to define a boolean
value:
Common operations:
#
Comparing ValuesIn 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
,
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.
#
Comparing Strings#
Comparing numbers#
Comparing tez๐ก Comparing
tez
values is especially useful when dealing with an amount sent in a transaction.
#
ConditionalsConditional 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 StatementJsLIGO 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
case
ordefault
.- If a
default
case is provided, It should be the last case.- Conditional
break
's are not supported i.e.break
inside aif-then-else
.- In case of nested
switch
statements, the innerswitch
should not contain areturn
.
You can run the quarter
function defined above using the LIGO compiler
like this: