What is the order of operations for logical operators?

Is there an order of operations for control flow? Like does the and come before the multiplication or is there an order for and, or, not, etc. ?

Logical operators have operator precedence the same as other operators (relational, arithmetic, etc.). The highest precedence belongs to not, followed by and, and finally by or.

Like other operations, grouping takes precedence, so we must evaluate bracketed expressions first, if they exist.

http://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html

27 Likes

Excuse me but how this operator precedence can be useful? What are some examples of using it actually?

Thank you in advance.

2 Likes
print(True or False and False)     # and has precedence
print((True or False) and False)   # parentheses change precedence

print(not False or True)        # not has precedence
print(not (False or True))      # parentheses change precedence

Output:

True
False
True
False
19 Likes

Thank you very much. Each time one of my questions is answered i get that "Ooooh now i get it " feeling!!!

11 Likes

Hello, would you explain to me why it prints only one statement?

If you’re referring to the order of operations performed by @patrickd314, then only output is gained after evaluating through the input expressions. So for example,

print (not False or True)

Not False is evaluated first to become “not False” aka True. After that the or operator takes two True values to output a single outcome of True.

Hope that answers your question!

1 Like

Yes, i understand it now, thank you. But for me the first part of

print(True or False and False)     # and has precedence
print((True or False) and False)   # parentheses change precedence

was more confusing.
It wasn’t obvious a little that these are not just expressions but parts that require certain answers to be “True” or “False”. For example:
“True and True” is True but “True and False” is False.
So expression with “and” needs to be only “True and True” to be True. And expression with “or” only requires one element to be “True” for it to be True.
In my experience at first i just looked at them as simple comparisons similar to what you can find in math and you just need to solve them. So i thought that “False and False” had to be “True” expression )

Gotcha. Yep, I was confused on this just like you mentioned for like the longest time until I briefly went through the introduction to logic course offered free from stanford:

http://intrologic.stanford.edu/public/index.php

I did not finish the whole thing as the thing becomes very theoretical and very complex towards the end, but covered enough to understand how the boolean operators work within logic. It’s a pretty good course to check out in general!

2 Likes

Wow, thank you very much!

Good question. Was wondering the same thing.

Thanks, for sharing.

well personally when i first saw i compared to what I know in math. Being that “and” has precedence. In the first expression “False and False” had to be answered first which is True and what’s left to process would be “True or True” which is True. In the second expression, parentheses has precedence so “True or False” would be answered first which is “True” and then “True and False”, which will answer “False”.

Hmmm…

Yeah… but you did the same mistake as i did )
False and False = “False”, not “True”.
That’s what i was talking about, mathematical approach ))