Newbie question, Need help to understand boolean

This is a discussion on Newbie question, Need help to understand boolean within the C++ Programming forums, part of the General Programming Boards category; I am using the tutorial: Cprogramming.com Tutorial: If Statements I can't seem to grasp one thing in this tutorial. Maybe ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    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.

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    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.
    Last edited by Chomp; 10-14-2010 at 09:42 PM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Thanks! That helped a ton!

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    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.
    Last edited by whiteflags; 10-14-2010 at 09:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 05:24 AM
  2. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  3. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 05:45 PM
  4. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 03:38 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21