A doubt about order of execution of statement

Nov 3, 2011 at 3:56pm
I can't figure out whether the statement will return true or false:
1
2
x = 1;
if ((x++) && (x--))

If I try it out, I get true. But I am not sure that whether the order in which the two are executed is defined or not.
So will it depend on compiler or is it defined?
Nov 3, 2011 at 4:04pm
&& is a sequence point and the operands are evaluated from left to right, so it's defined.
Nov 3, 2011 at 4:05pm
If you really want to know, switch the statement around. Now, the value of 'x' never goes below 1 (thus always true), thus the statement reduces to either "2 && 1" (if the brackets are handled first) or "1 && 1" (if brackets are ignored). Switching the statement around would either result to "1 && 1" or "0 && 1".

To be honest, I don't really see a good reason to have two increment or decrement operators on the same variable in a single statement. Even if it provides the result you want, it's definitely not going to improve readability.
Nov 3, 2011 at 4:07pm
@Athar:
Thanks a lot!
@Gamnic:
I am not using it anywhere at all! It was sheer curiosity...
Topic archived. No new replies allowed.