Not, Or, And statments

This is a discussion on Not, Or, And statments within the C++ Programming forums, part of the General Programming Boards category; I'm barely beginning to use C++ and I wanted to know how I am supposed to read the statement: A. ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    6

    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. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    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

    Now on to your questions:

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

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

    C. !( ( 1 || 0 ) && 0 )
    1 OR 0 is 1
    1 AND 0 is 0
    NOT 0 is 1 therefore
    ANSWER: 1
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    6

    Thanks

    thank you so much for your help

Popular pages Recent additions subscribe to a feed

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