Skip to main content
Version: Next

Booleans

The predefined type bool has exactly two values: true and false.

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

Or

The logical disjunction ("or") is implemented by the binary operator ||.

const or_1: bool = false || true; // true
const or_2: bool = false || false; // false
const or_3: bool = true || true; // true
const or_4: bool = true || false; // true

And

The logical conjunction ("and") is implemented by the binary operator &&.

const and_1: bool = false && true; // false
const and_2: bool = false && false; // false
const and_3: bool = true && true; // true
const and_4: bool = true && false; // false

Not

The logical negation ("not") is implemented by the unary operator !.

const not_1: bool = !true // false
const not_2: bool = !false // true

Comparing

Boolean values are the result of comparisons of values. Numbers and strings are completely ordered. Booleans can be compared for equality. Two values need to be of the same type to be compared, but not all values of the same type can be compared: only those with comparable types (a concept directly lifted from Michelson) such as int, nat, string, and bool itself. The comparison operators are overloaded so they are defined on all comparable types.

const a: bool = 1 == 1; // equality (true)
const b: bool = 1 != 0; // inequality (true)
const c: bool = 1 > 0; // greater than (true)
const d: bool = 0 < 1; // lower than (true)
const e: bool = 0 >= 0; // greater than or equal (true)
const f: bool = 0 <= 0; // lower than or equal (true)

Conditional expressions

Conditional logic enables forking the control flow depending on the state, that is, the values available at a given point in the code. Put in a less technical manner, conditionals enable decision making.

A conditional expression is made of three parts:

  1. a condition, that is, a boolean expression;
  2. an expression evaluated if, and only if, the condition is true;
  3. an expression evaluated if, and only if, the condition is false.

The syntax uses a ternary operator with the symbols ? and : to separate the three parts:

const a = 0;
const b = 1;
const min = (a < b) ? a : b; // min == 0

Note: Parentheses are often necessary before ?, but not always: you can either rely on the compiler error message or always use parentheses.