# Newbie question, Need help to understand boolean

• 10-14-2010
mlabcdzs
Newbie question, Need help to understand boolean
I am using the tutorial:
Cprogramming.com Tutorial: If Statements

I can't seem to grasp one thing in this tutorial. Maybe I am focusing on the wrong thing or something, but here it is.

At the end of the tutorial it is talking about Boolean operators.
Code:

```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)```
How do you get the answers to that?
I am trying to make my question as clear as I can.

How can you take !(1 || 0) and say the answer is 0 ?

Can someone maybe explain it in a different approach or something so that I can understand it?
Maybe how to evaluate that step by step?

I think I know this much:

! = NOT
|| = OR
NOT is evaluated before AND OR
AND = is evaluated before OR

I understand everything in the previous tutorial's. I just feel if I don't grasp this I will have problems understanding future things. I have written and manipulated beginner code from the previous tutorial's just fine.
• 10-14-2010
Chomp
This is known as Boolean Logic.

1 represents true and 0 represents false.

In the first evaluation, true or false evaluates to true; therefore, not true or false or !(1 || 0) evaluates to 0.

In the second evaluation, we know that and evaluates before or. So, we can place parenthesis to make it a little more clear and go from there.

Code:

`!( 1 || 1 && 0 ) == !( 1 || ( 1 && 0 ) )`
So, taking it piece by piece, the first evaluation will be 1 && 0 (true and false) which evaluates to false. So, we can replace that evaluation with a 0 to get:

Code:

`!( 1 || 1 && 0) == !( 1 || ( 0 ) )`
which is reduced to true or false, the same as the first evaluation, which we know evaluates to true. Therefore, since this is negated by the !, this is false or 0.

In the final evaluation, we have:

Code:

`!( ( 1 || 0 ) && 0 )`
Following from the second example, we will evaluate the part in parenthesis first which is our familiar friend true or false and we know that evaluates to true. So, let's replace that to get:

Code:

`!( 1 && 0 )`
We know that true and false is false, so we negate that to get true.

I hope this helps.
• 10-14-2010
mlabcdzs
Thanks! That helped a ton!
• 10-14-2010
whiteflags
Actually, I am fairly sure that the compiler does this:

!( 1 || 1 && 0 ) == !( 1 || ( 1 && 0 ) )
!( 1 || 0 ) == !( 1 || ( 1 && 0 ) )
!( 1 ) == !( 1 || ( 1 && 0 ) )
0 == !( 1 || ( 1 && 0 ) )
0 == !( 1 || 0 )
0 == !( 1 )
0 == 0
1

So looking at it a different way (and not to be mean, but the correct way) that's what happens with Chomp's example.