Version: 0.19.0

Booleans and Conditionals

Booleans#

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

let a: bool = true;
let b: bool = false;

Common operations:

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

Comparing Strings#

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

Comparing numbers#

let a: int = 5;
let b: int = 4;
let c: bool = (a == b);
let d: bool = (a > b);
let e: bool = (a < b);
let f: bool = (a <= b);
let g: bool = (a >= b);
let h: bool = (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: bool = (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 : nat) : magnitude => {
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 evaluate-call
gitlab-pages/docs/language-basics/src/boolean-if-else/cond.religo compare '21n'
# Outputs: Large