Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logical OR vs Logical AND: which should be more binding?

I'm writing a small parser, which will have an OR operator and an AND operator. When you see a series of ORs and ANDs, which do you expect will be more binding? Given the expression a & b | c, do you expect it to mean (a&b)|c or a&(b|c)? Can you give any reason to prefer one over the other?

like image 426
Null Set Avatar asked Jan 20 '23 18:01

Null Set


1 Answers

Do what everyone else does; AND binds tighter than OR (see e.g. C Operator Precedence Table). This is the convention that everyone expects, so adopt the principle of least surprise.

This choice isn't arbitrary. It stems from the fact that AND and OR follow a similar relationship to multiply and add, respectively; see e.g. http://en.wikipedia.org/wiki/Boolean_logic#Other_notations.

Note also that users of your language should be heavily encouraged to use parentheses to make their intentions clear to readers of their code. But that's up to them!

like image 57
Oliver Charlesworth Avatar answered Jan 30 '23 04:01

Oliver Charlesworth