Regards
Salil Verma
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
these two numbers should agree. the correct answer is 88, not 90.
Do you see what I mean?
x = x++ + x++ = 91 // Example 1
x = x++ + y++ - ++y = 89 // Example 2
int x = 45;
int y = 12;
--> x is now 45 and y is now 12
x = x++ + x++;
--> add precedence
x = (x++) + (x++);
--> substitute first part...
x = (45) + (x++);
--> x is now 46; substitute second part...
x = (45) + (46);
--> x becomes 47 after increment, and then becomes 91 after sum and assigment. Next...
*** This is where the first x is printed. When it has a value of 91 ***
x = x++ + y++ - ++y;
--> add precedence
x = (x++) + (y++) - (++y);
--> substitute first part
x = (91) + (y++) - (++y);
--> x is now 92. substitute second part.
x = (91) + (12) - (++y);
--> x is now 92. y is now 13. substitute third part.
x = (91) + (12) - (14);
--> x is now 92. y is now 14 (and since preincrement, 14 is used).
x = 89;
--> x is now 89 and y is now 14. next...
*** This is where the second x is printed. When it has a value of 89 ***
a = (b=x++) + (c=x++) = 91
a = 91
b = 45
c = 46
x = 47
int x = 45;
int y = 12;
--> x is now 45 and y is now 12
x = x++ + x++;
--> add precedence
x = (x++) + (x++);
--> substitute first part...
x = (45) + (x++);
*** The value of b is the first paren, which is 45 ***
--> x is now 46; substitute second part...
x = (45) + (46);
*** The value of c is the second paren, which is 46 ***
--> x becomes 47 after increment, and then becomes 91 after sum and assigment. Next...
*** The value of a should be the value of x at this point, has a value of 91 ***
*** The value of x should also be 91, but it wasn't assigned in the altered example, so has a value of 47 ***
d = (h=((e=a++) + (f=y++))) - (g=++y) = 89
h = 103
e = 91
f = 12
a = 92
y = 14
g = 14
x = x++ + y++ - ++y;
--> add precedence
x = (x++) + (y++) - (++y);
--> substitute first part
x = (91) + (y++) - (++y);
*** The value of e is the first paren, which is 91 ***
--> x is now 92. substitute second part.
*** the value of a is now 92, as you are using a instead of x ***
x = (91) + (12) - (++y);
*** The value of f is the second paren, which is 12 ***
*** The value of h is the first two parens, with is 91 + 12 = 103 ***
--> x is now 92. y is now 13. substitute third part.
x = (91) + (12) - (14);
*** the value of g is the third paren, which is 14 ***
--> x is now 92. y is now 14 (and since preincrement, 14 is used).
x = 89;
--> x is now 89 and y is now 14. next...
*** and the value of y ends at 14 ***
Harry Henriques wrote:
Just take for granted that I coded the examples correctly, and look at the output in RED. I'm pretty sure I didn't make any stupid mistakes.
If you will take a look at "the dozen variables", maybe you'll notice that there is something strange happening in the two examples that I listed.
Harry Henriques wrote:In the first example, "x" is post incremented before it is added to the final x++ in the expression (x++ + x++). In the second example, (x++ + y++), "x" is added to "y" before "x" is post incremented and before "y" is post incremented.
K. Tsang CEng MBCS PMP PMI-ACP OCMJEA OCPJP
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |