Thread: First post. Confused about Boolean

  1. #1
    Registered User Dagon's Smile's Avatar
    Join Date
    Feb 2014
    Posts
    4

    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. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

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

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    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
    Devoted my life to programming...

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by phantomotap View Post
    Associativity in C++ can be a pain...
    Are you some kind of paranoid associativity fanboy? I think you mean precedence.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User Dagon's Smile's Avatar
    Join Date
    Feb 2014
    Posts
    4
    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. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Dagon's Smile View Post
    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.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User Dagon's Smile's Avatar
    Join Date
    Feb 2014
    Posts
    4
    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. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    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
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    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).
    Last edited by rcgldr; 02-04-2014 at 12:39 AM.

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by phantomotap View Post
    Are you some kind of buffoon?
    I didn't realize you were so fragile.
    You sure like name-calling, though.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    Quote Originally Posted by oogabooga View Post
    Are you some kind of paranoid associativity fanboy? I think you mean precedence.
    Quote Originally Posted by phantomotap View Post

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

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Hodor, hodor, hodor!
    O_o

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

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boolean.c
    By wise_ron in forum C Programming
    Replies: 7
    Last Post: 10-27-2006, 07:02 AM
  2. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  3. Replies: 4
    Last Post: 04-02-2004, 07:30 PM
  4. New, and need Boolean help!
    By ivanlucrazy in forum C++ Programming
    Replies: 10
    Last Post: 10-21-2003, 02:13 PM