Version: 0.62.0

Booleans and Conditionals

Booleans​

The type of a boolean value is `bool`. Here is how to define a boolean value:

let a = true;
let b = false;

Common operations:

&&
Logical and
let logical_and = true && true;
||
Logical or
let logical_or = false || true;
!
Logical not
let logical_not = !false;
==
Equals
let eq = 2 == 3;
!=
Not equals
let not_eq = 2 != 3;
>
Greater than
let gt = 4 > 3;
<
Less than
let lt = 4 < 3;
>=
Greater than or equal to
let gte = 4 >= 3;
<=
Less than or equal to
let lte = 4 <= 3;

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`, `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​

let a = "Alice";
let b = "Alice";
let c = (a == b); // true

Comparing numbers​

let a = 5;
let b = 4;
let c = (a == b);
let d = (a > b);
let e = (a < b);
let f = (a <= b);
let g = (a >= b);
let h = (a != b);

Comparing tez​

💡 Comparing `tez` values is especially useful when dealing with an amount sent in a transaction.

let a: tez = 5 as mutez;
let b: tez = 10 as mutez;
let c = (a == b); // false

Conditionals​

Conditional logic enables forking the control flow depending on the state.

type magnitude = ["Small"] | ["Large"]; // See variant types.
let compare = n => {
if (n < (10 as nat)) {
return Small ();
}
else {
return Large ();
};
};

You can run the `compare` function defined above using the LIGO compiler like this:

ligo run evaluate-call
gitlab-pages/docs/language-basics/src/boolean-if-else/cond.jsligo '21n' --entry-point compare
# Outputs: Large

Switch Statement​

JsLIGO also supports branching of control flow via the switch statement.

let quarter = n => {
let output = "";
switch (n) {
case 1:
case 2:
case 3:
output = "Q1";
break;
case 4:
case 5:
case 6:
output = "Q2";
break;
case 7:
case 8:
case 9:
output = "Q3";
break;
case 10:
case 11:
case 12:
output = "Q4";
break;
default:
output = "Invalid month."
};
return output;
}

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

1. A switch should have at-least one `case` or `default`.
2. If a `default` case is provided, It should be the last case.
3. Conditional `break`'s are not supported i.e. `break` inside a `if-then-else`.
4. In case of nested `switch` statements, the inner `switch` should not contain a `return`.

You can run the `quarter` function defined above using the LIGO compiler like this:

ligo run evaluate-call
gitlab-pages/docs/language-basics/src/boolean-if-else/switch.jsligo '5' --entry-point quarter
# Outputs: "Q2"

Ternary expression​

JsLIGO also supports JavaScript's ternary expression:

let ternary = a => a == 1 ? true : false

which can also be nested:

let ternary_nested = a =>
a == 1 ? "one" :
a == 2 ? "two" :
a == 3 ? "three" :
"other"