# 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?

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`.

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 ))