# Thread: Not, Or, And statments

1. ## Not, Or, And statments

I'm barely beginning to use C++ and I wanted to know how I am supposed to read the statement:

A. !( 1 || 0 ) ANSWER: 0
B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)
C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

I don't understand how the examples came up with the answers so can someone write out how I am supposed to read it?

2. Sounds like you need to study up on some boolean tables

OR
---------------
0 OR 0 is 0
0 OR 1 is 1
1 OR 0 is 1
1 OR 1 is 1

AND
---------------
0 AND 0 is 0
0 AND 1 is 0
1 AND 0 is 0
1 AND 1 is 1

NOT
---------------
NOT 0 is 1
NOT 1 is 0

A. !( 1 || 0 )
1 OR 0 is 1
NOT 1 is 0 therefore...

B. !( 1 || 1 && 0 )
1 AND 0 is 0 (AND is evaluated before OR)
1 OR 0 is 1
NOT 1 is 0 therefore...

C. !( ( 1 || 0 ) && 0 )
1 OR 0 is 1
1 AND 0 is 0
NOT 0 is 1 therefore

3. Its helpful to replace 1 with T for true and 0 with F for false.
A) you do the parenthesis first: So you get T || F, so you get T. The not (!) of T is F.
B) You have to be careful here. the && is not evaluated before the ||. Its just that && binds the results tighter.
So you get: T || F which is T not is F
C) I'll be verbose in this one
!( (T || F) && F )
!( T && F )
!(F)
T

More indepth for B:
C and C++ ensure that in a boolean statement (which this is) that everything will be evaluated from Left to Right and that it will stop as soon as it has enough information to get an answer.

So given:
X || Y && Z
If X == true then Y and Z is never evaulated because it has enough information to know the answer is true.

This is useful if you want to make sure that certain things are true(or false) before evaluating the rest of the expression.
Example:
Code:
```if ( x % y  > 3 )
// Do something```
Possible problem: if y is zero then you'll get a division by zero error. Easy fix:
Code:
```if ( y!=0 && x % y > 3)
// Do something```
You've now just made sure that you won't have division by zero at that point.

4. ## Thanks

thank you so much for your help