Help with tutorial
New user to C/C++ here and I have been going through the tutorials on this site to better understand it. I am confused by the following section though and am hoping someone will help me understand it.
To specify, what I do understand is the operators themselves, but don't appreciate what is being performed in each of the above cases. Meaning, I am aware of how comparison operators can be used to check the value of variables, but why does the statement [NOT (1 OR 0)], by itself, return anything at all? I guess I'm confused as to why any of the above statements (A, B, or C) aren't completely useless, and why they conclude with a value. Hopefully that makes sense.
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)
Thanks in advance for any help.
I think you need to look at how boolean algebra works in C and C++. Anything non zero is regarded as true (positive and negative values). Zero is regarded as false. Therefore, when applying boolean operators (&& ||), just consider each value as true if non-zer and false if zero.
So, for A
NOT(TRUE OR FALSE)
I am not going to go through them all. Hope that helps.
You should read up on boolean operators, in a book or maybe somewhere in here: http://www.cprogramming.com/tutorial.html
All the C++ boolean operators return values. This is actually to simplify things. The operators can then be regarded as ordinary math operators like + and - which take arguments and return values. When you learn classes and operator overloading you will find returning values from boolean operators very useful. In fact, if(X) will just check if X does not evaluate to zero, which is why you can use if(x) instead of if(x != 0) in code.
If && didn't return anything, how could you do this?
if(x || (y && z))
Think about it.