I ran into a situation like this:
if(true,false)
{
cout<<"A";
}
else
{
cout<<"B";
}
Actually it writes out B. How does this statement works? Accordint to my observation always the last value counts. But then what is the point of this?
Thanks
from http://www.cplusplus.com/doc/tutorial/operators/
The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the right-most expression is considered.
For example, the following code: a = (b=3, b+2);
would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end, variable a would contain the value 5 while variable b would contain value 3.
So here
if(true,false)
{
}
evaluates to if(false)
According to http://www.cplusplus.com/doc/tutorial/operators/
The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the right-most expression is considered.
So for example consider the following:
int a, b;
a = (b=3, b+2);
b gets set to 3, but the equals operator only cares about the second half so the actual value returned is 5. As for the usefulness? That's conditional :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With