1. ## First post. Confused about Boolean

Hello, this is my first post. I am 28 years old and I have always wanted to learn programming but I am only now attempting to take a first step. I have been reading the main C++ tutorial on the main site and I am stuck at the for loops. I understand what they are and how to use them on a basic level but the last portion of the tutorial mentions the Boolean operators !, &&, and ||. I know how to use these a little but the whole true false thing confuses me. I understand that 0 is false and 1 is true but I get confused on the evaluation part. Here is an example used in the tutorial

!( 1 || 1 && 0 )

The way am reading this is "not 1 or 1 and 0". The tutorial says it evaluates to 0. So is it like this:

not 1 is 1 therefore it is 0(false) and the second part just completely confuses me. How is it not 1 and 0 but it is either a 1(true) or 0(false)? I did not get the way it was explained. I guess what I am asking for is a dumbed down explanation please?

2. O_o

Associativity in C++ can be a pain...

Soma

3. Like multiplication has a higher precedence than addition, the same goes for boolean AND vs boolean OR.
In your example, the "1 && 0" happens first, giving 0 and then "1 || 0" gives 1, finally "!(1)" gives 0.
The precedence with boolean operators is( from higher to lower ): NOT, AND, OR

4. You need to consider the precedence and the parentheses. The part in the parens is computed first.
Within the parens, the && has higher precedence, so the first thing that's computed is 1 && 0, which is 0. This yields
!(1 || 0)
Then inside the parens is 1 || 0 which is 1, yielding
!1
And that gives
0

5. Originally Posted by phantomotap
Associativity in C++ can be a pain...
Are you some kind of paranoid associativity fanboy? I think you mean precedence.

6. OK, so 1&&0 is computed first but why is it 0? this is what is confusing me. How does the statement "One and zero" equal false?
Then 1||0 is yields 1. Why? Again I read that part of the tutorial but I don't get it.
I get the last part !1 this is self evident. It's the rest

7. Originally Posted by Dagon's Smile
OK, so 1&&0 is computed first but why is it 0? this is what is confusing me. How does the statement "One and zero" equal false?
Then 1||0 is yields 1. Why? Again I read that part of the tutorial but I don't get it.
I get the last part !1 this is self evident. It's the rest
&& yields 1 if and only if both it's operands are non-zero; otherwise it yields 0.

|| yields 0 if and only if both of it's operands are 0; otherwise it yields 1.

8. OK, Thanks. This cleared up my confusion. The worst part is this was clearly explained in the tutorial. I did not link the 2 ideas to their examples and now I feel foolish. Thanks though now I can move on to while loops.

9. Are you some kind of paranoid associativity fanboy?
O_o

Are you some kind of buffoon?

I'll spell it out for you: I was telling the original poster to search for associativity in C++ because that would take him to "short-circuiting" which would have answered his question.

Because here is a news flash for you: you and GReaper are both wrong!

Code:
```!( 1 || 1 && 0 )
/*
!( x || y && z ) // labels
*/```
Because `x' is true, the expression `y && z' is NEVER EVALUATED AT ALL!

The code example evaluates to false simply because `!(true)' is `false'.

Soma

10. To clarify the previous post, the logical operators || and && are "short circuiting" or "early out" operators, and the order of evaluation is left to right despite any precedence in the expressions. If the left side of an "short circuiting" operator determines the result, then the right side is not evaluated. In this case (1 || ... ) is going to evaluate to TRUE (1) no matter what the expression is to the right of ||, so the expression to the right of || is not evaluated. Continuing with this example, then !(1 || ...) returns a FALSE (0).

11. Originally Posted by phantomotap
Are you some kind of buffoon?
I didn't realize you were so fragile.
You sure like name-calling, though.

12. Originally Posted by oogabooga
Are you some kind of paranoid associativity fanboy? I think you mean precedence.
Originally Posted by phantomotap

Are you some kind of buffoon?
Originally Posted by oogabooga
I didn't realize you were so fragile.
You sure like name-calling, though.
Hodor, hodor, hodor!

13. Hodor, hodor, hodor!
O_o

No Hodor, mommy and daddy aren't getting a divorce; we still love you.

Soma