Logical expressions

Logical expressions are used to express common logical operations on values of type Bool. There are three types of logical expressions in Motoko.

not expression

The not expression takes only one operand of type Bool and negates the value:

let negate : Bool = not false;

let yes : Bool = not (1 > 2);

In the first line negate has boolean value true. It is set by negating the boolean literal false. In the second line, we negate the boolean expression (1 > 2).

Both negate and yes are of type Bool. This type is inferred.

The truth table for not is

xnot x
truefalse
falsetrue

and expression

The and expression takes two operands of type Bool and performs a logical AND operation.

let result : Bool = true and false;

result is now false according to the truth table.

xyx and y
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

or expression

The or expression takes two operands of type Bool and performs a logical OR operation.

let result : Bool = true or false;

result is now true according to the truth table.

xyx 0r y
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse