Frankly, I am still very confused with the rule for the usage of the ++ and -- in the expression.
Just try some execise in the g++, it seems that the rule for ++/-- is:
As post-increment (X++) or post-decrement (X--), it alaways takes effect after the whole expression is done, even after the assignment is completed;
As pre-increment (X++) or pre-decrement (X--), the ++/ -- firstly takes effect at the place where it is, and then the expression is executed.
For example:
A)
int incr = 3;
incr = (incr++)*4 + (incr++)*3 + 4;
After the execution, the incr is 27.
Steps:
1. incr = 3*4 + 3*3 + 4 = 25;
2. incr = incr + 2 = 27.
B)
incr = 3;
incr = (incr--)*4 + (incr--)*3 + 4;
Result incr is 23;
C)
incr = 3;
incr = (--incr)*4 + (--incr)*3 + (++incr) * 4;
Result is 19.
Steps:
1. incr = (3-1)*4 + (2-1)*3 + (1+1)* 4 = 19
D)
incr = 3
incr = (--incr)*4 + (--incr)*3 + (++incr) * 4 + (incr++);
Result is 22
Steps:
1. incr = (3-1)*4 + (2-1)*3 + (1+1)*4 + 2 = 21
2. incr ++ -> incr = 22.
REMARK Here!