Thread: Newbie with C++ NOT, OR, AND operators question...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    4

    Newbie with C++ NOT, OR, AND operators question...

    Hello,

    This is my first post on Cprogramming.com as i have just got started with learning C++ from the tutorial area on this website which at the moment is going quite good. However i'm onto the part what explains about the NOT, AND, OR operators. I'm at the last section of the IF statements page currently where it gives an example of the following below:

    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)
    Does the first example (A) does the OR operator take place first? So the 1 || 0 would be TRUE and then the NOT operator comes in and changes it to False which gives it a value of 0?

    Is that how it works?

    many thanks, Josh.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes, because the parenthese establish the order here.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    4
    Ok thanks, i now know how to first to examples work but i don't quite understand how the last example works:

    C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)
    What would come first in that? the && operator has the value of 0, does that mean it's false? and then the OR operator is true but doesnt the NOT operator make it false after the OR operator has made it True, i'm confused to how it would get the ANSWER: 1 because wouldnt the NOT operator set it back to false?

    thanks, Josh.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All paranthesises goes first. Just like math.

    First (1 || 0) is evaluated.
    Then the answer of that && 0 is evaluated.
    Then that !answer is evaluated.
    So:

    (1 || 0) --> 1
    1 && 0 --> 0
    !0 --> 1

    Easy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    4
    Hello,

    Thanks! I see how it work's now, just didn't click too well when i first saw it.

    Thankyou, Josh.

Popular pages Recent additions subscribe to a feed