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
x | not x |
---|---|
true | false |
false | true |
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.
x | y | x and y |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
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.
x | y | x 0r y |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |