Version: 0.28.0

Booleans and Conditionals


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;
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


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 run evaluate-call
gitlab-pages/docs/language-basics/src/boolean-if-else/cond.religo '21n' --entry-point compare
# Outputs: Large

Switch Statement#

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

let quarter = (n : int) : string => {
let output = "";
switch (n) {
case 1:
case 2:
case 3:
output = "Q1";
case 4:
case 5:
case 6:
output = "Q2";
case 7:
case 8:
case 9:
output = "Q3";
case 10:
case 11:
case 12:
output = "Q4";
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"