Booleans
The predefined type bool
has exactly two values: true
and false
.
Or
The logical disjunction ("or") is implemented by the binary operator
||
.
And
The logical conjunction ("and") is implemented by the binary operator
&&
.
Not
The logical negation ("not") is implemented by the unary operator
!
.
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.
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:
- a condition, that is, a boolean expression;
- an expression evaluated if, and only if, the condition is true;
- 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:
Note: Parentheses are often necessary before ?
, but not always: you
can either rely on the compiler error message or always use
parentheses.