Logical operators

Hello my Dears))

Are there any logical operators like [AND], [OR], [xOR] exists in Motoko language?
If yes, where can I read about them?
if not, what are the alternative ways to imitate them?

1 Like

You can use and and or, eg
if (a and b) return something

You can also find bitwise operators here: https://sdk.dfinity.org/docs/developers-guide/basic-syntax-rules.html#_bitwise_binary_operators

You’ll find a lot of other good reference material in the language guide too, explore the menu on the left from here: https://sdk.dfinity.org/docs/language-guide/about-this-guide.html

Unfortunately, they are doesn’t work in this situation.
when I try this:
if ((a0 > 0) | (a1 > 0)) {};

the compiler raises that error

type error [M0060], operator is not defined for operand types
    Bool
and
    Bool

if ((a0 > 0) or (a1 > 0)) {}; should do the trick.

1 Like

| exists but is used for bitwise operations on integers and naturals, not on booleans (though we could add them)

These links might help:

Motoko language manual:

Motoko operators:

Yes, exactly.

And additionally there are [and], [or] keywords reserved for booleans comparison according to @Ori.
Also I tried them and it worked

Another thing to point out is that ‘and’ and ‘or’ are short-circuiting/lazy and will only evaluate their right hand operand when necessary.

Thus ‘true or (loop {})’ evaluates to ‘true’ , and doesn’t loop.

3 Likes