1. ## Evaluating Operators

Hi, I am new to the message board here, and brand new to C++. I'm trying to learn how to do this on my own, to better my life. Anyhow, here is my question:

the statement !(1 && !(0 || 1)) is a true statement.

Why?

Could someone explain this statement to me and how its evaluated? Thanks in advance!

2. What do you know about these operators? How would you go about solving it?

Do you know that in this case 1 is true and 0 is false?

Do you know what order of operations means?

Do you know what 1 || 0 evaluates to? How about 1 && 0? How about !1?

Do you know how parentheses affect the order of evaluation?

3. Start with what's in the innermost set of parenthesis (the OR):
!(1 && !(0 || 1)) = !(1 && !(1))

Next, evaluate the NOT(!):
!(1 && !(1)) = !(1 && 0)

Next evaluate the AND:
!(1 && 0) = !(0)

Last, evaluate the NOT(!):
!(0) = 1

4. Remember that && and || use short-circuit evaluation. The RHS might not even be evaluated.

If you had !(0 && !(0 || 1)) for example, the red part of the expression would not even be evaluated ( despite the extra () ). The LHS side of the && is false, therefore the whole thing is false regardless of what the RHS is.

This is to allow you to test and dereference a pointer for example, with complete safety.
char *p;
if ( p && *p ) doStuff(*p);

First checks that the pointer is not NULL, then checks that the char being pointed at isn't zero.

Boolean expressions are read L->R, not starting with the most deeply nested ( ) sub-expression.

5. Originally Posted by hk_mp5kpdw
!(1 && !(0 || 1)) = !(1 && !(1))

Next, evaluate the NOT(!):
!(1 && !(1)) = !(1 && 0)

Next evaluate the AND:
!(1 && 0) = !(0)

Last, evaluate the NOT(!):
!(0) = 1

Thank you for your help hk. Very useful explanation, it helped A LOT.

6. Well, Salem, that allows me to simplify a great deal of my code.