There's this line of code that I don't understand:
1 2 3 4 5 6
int a=0, b=2, c=4;
if (a = c-b)
cout<< b << endl;
else
cout<< c << endl;
cout << (a == b-c);
First, a isn't equal to c-b, so why would it cout b? and also I thought the last line would cout a 2, but that's not the case. can someone please explain why?
Is that if condition correct? You usually don't want to assign values in an if condition, you want to compare values. Is it meant to compare the value of a (0) to c-b (2)?
a == c-b isn't the comparison being done there, it's a == b-c.
First, a isn't equal to c-b, so why would it cout b?
You seemed to be expecting it to be making a comparison and that's what would usually happen in an if condition. It's a common typo to use = when == should be used, that's why I asked if the code was intended to assign or compare.