Applying Order Of Boolean Precedence
I'm just wishing to clarify my understanding of how the order of boolean precedence is applied in expressions. I'll give an example using the following order of precedence:
1) NOT
2) AND
3) OR
Given the expression, A NOT B AND C
, I know that NOT
will be evaluated first. However, will this be applied to the rest of the expression (B AND C)
, or just the next token, B
?
My current understanding is that it is the former so this should be evaluated as A NOT (B AND C)
. Is this correct开发者_Python百科 ?
Thanks
There is no universally agreed order of precedence for boolean operators, so it will depend on who or what is doing the parsing. For any given computer language this should be well defined in the documentation for the language. Also note that NOT
is a unary operator (with right-to-left associativity), so your example above is not really a valid boolean expression.
It could be argued that since boolean operators have analogues in ordinary arithmetic, that we should use an analogous order of precedence. In that case NOT
(analogous to unary negation) would have the highest precedence, then AND
(multiplication), then OR
(addition).
精彩评论