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

• 07-27-2008
JiWiz
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:

Quote:

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.
• 07-27-2008
MacGyver
Yes, because the parenthese establish the order here.
• 07-27-2008
JiWiz
Ok thanks, i now know how to first to examples work but i don't quite understand how the last example works:

Quote:

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.
• 07-27-2008
Elysia
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.
• 07-27-2008
JiWiz
Hello,

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

Thankyou, Josh.