Does the order of logical operators matter?


#1

Question

If we are combining multiple logical statements, and we kept the statement order fixed, does the order of the logical operators (or/and) matter?

Answer

Yes and no. It depends on how many logical statements there are.

When there is only one logical statement, it’s always going to evaluate the same way, because it’s just a single statement.
num < 3
num > 6

When there are two logical statements, order does not matter, because we can only apply one logical statement & (and) or | (or) between them.
(num < 3) | (num > 6)
is the same as
(num > 6) | (num < 3)

When we have more than two statements, that is when order does start to matter.

Say that we have three statements, and wish to combine them using one | and one &. Like so, in different operator orders.

A & B | C
A | B & C

These will not necessarily be the same result. For instance, take these values.

A = False
B = False
C = True

A & B | C
False & False | True = True
A | B & C
False | False & True = False

So, order of the logical operators will matter. This is mainly due to the order of operations, as & (and) is evaluated before any | (or) operators.