OR, AND and FALSE


#1

There was an earlier post on this issue but it was locked.

In exercise 4: Simple Errors; -- while choice != 'y' and choice != 'n':-- works, but if you substitute OR for AND, it doesn't play out right. I understand if you add NOT you can make it work, but...

My question is; does OR not work because one of the conditions has to be False due to the nature of the OR condition?

If this is the case, why does it still execute some of the loop? Wouldn't a False condition prevent the loop from even starting?

Just trying to wrap my head around it a little better. Thanks for any feedback.


#2

It has to do with short-circuiting. AND is short-circuited by False and OR is short-circuited by True.

For an AND expression to yield True, both operands MUST be true.

For an OR expression to yield False, both operands MUST be false.

x != 'y' and x != 'n'

when True means x is neither 'y' nor 'n'.

x != 'y' or x != 'n'

when True means that one of the operands is true. If x is not equal to 'y' it will short-circuit and not even evaluate the second operand.


#3

Thanks for the clear answer!


#4

I was reading about some other concepts and came across the terms inclusive and exclusive and that gave me another question concerning this.

If I understand correctly, that makes OR, inclusive? Is that correct?

If that is true, are there any examples of an exclusive OR, in python?

I'm not certain exclusive and inclusive are the best words to use for this question but it made sense to frame it that way. Some other related topics spoke about it in this context and apparently some languages use both 'versions' of OR to some extent.

Here is a source for context from StackOverflow.


#5

As we mentioned above, OR short-circuits on true. This is an inclusive expression since both operands may be true. An exclusive OR (written as XOR) is one where both cannot be true.

Given A XOR B

(A or B) and not (A and B)

is an exclusive OR logical expression when either A is true, and not B, or B is true, and not A..

Where A and B are both boolean values, A XOR B is the equivalent of A != B.


#6

Thank you, my friend!


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.