This is a question from the SCJP 5.0 WhizLabs Diagnostic exam. Unfortunately, I didn't write down the answers in the exam. Will someone tell me how to calculate the answer for this and similar problems?

I realize that in the case of x++ and y--, the values of x and y are returned BEFORE the increment or decrement operation. In the case of ++x and --y, the values are returned AFTER the increment or decrement operation. This realization doesn't help me understand how to calculate the final result.

Thanks,

Harry

C:\Documents and Settings\Harry Henriques>java Expo

int x = 45

int y = 12

int a = 45

int b = 45

int c = 45

int d = 12

a = a + 1 = 46

b = a - 1 = 45

int bb = a + b = 91

int cc = ((c++) + (c--)) = a + b = 91

x = x++ + x-- = 91

x = x++ + y-- - ++y = 91

int dd = ((bb++) + (d--) - (++d)) = 91

System.out.println( --x); prints 90

System.out.println( --dd); prints 90

This is the basic problem.

I have expanded on the problem in the code block below.

C:\Documents and Settings\Harry Henriques>java Expo

int x = 45

int y = 12

int a = 45

int b = 45

int c = 45

int d = 12

a = a + 1 = 46

b = a + 1 = 47

int bb = a + b = 93

int cc = ((c++) + (c++)) = 91

x = x++ + x++ = 91

x = x++ + y++ - ++y = 89

= 92 + 13 - 14 = 91 ???

e = e + 1 = 13

f = 1 + e = 14

ee = bb + 1 = 94

ff = ee + e - f = 93

ff =((bb+=1) + (e+=1) - (e+=1)) = 93

int dd = ((bb++) + (d++) - (++d)) = 91

System.out.println( --x); prints 88 // these two numbers should agree. the correct answer is 88, not 90.

System.out.println( --dd); prints 90

We just have to be clear that in x++, the value of x used is the PRE-INCREMENTED value (value before increment) and in ++x, it the POST-INCREMENTED value (value after the increment).

Then all you need to do is keep replacing x's value in the expression.

Let us say x = 3.

(x++) + (++x) becomes:

3 + (++4), which is 3 + 5, which is 8.

Hope that helped.

To walk through the "basic problem"...

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...

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...

System.out.println(--x);

--> x is now 88. and since predecrement is used 88 is printed.

Henry

Harry

C:\Documents and Settings\Harry Henriques>java Expo1

x = x++ + x++ = 91 // Example 1

x = x++ + y++ - ++y = 89 // Example 2

a = 0

b = 0

c = 0

x = 45

a = (b=x++) + (c=x++) = 91

a = 91

b = 45

c = 46

x = 47

d = (h=((e=a++) + (f=y++))) - (g=++y) = 89

h = 103

e = 91

f = 12

a = 92

y = 14

g = 14

Henry

x = x++ + x++ = 91 // Example 1

x = x++ + y++ - ++y = 89 // Example 2

From the walkthrough...

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 ***

Henry

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. 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.

How are we supposed to answer questions like this on the SCJP exam? I have found a question like this on two different mock exams. This is like a throw away question. I don't know how to predict the outcome. What would happen if you had a problem like x = x++ + x++ + y++ - ++y or x = x++ + ++x + x++ + y-- I know this question may seems ridiculous, but if there is a "rule" that can be applied in these situations, I would like to know what it is?

Harry

Your output...

a = (b=x++) + (c=x++) = 91

a = 91

b = 45

c = 46

x = 47

From the walkthrough...

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 ***

Henry

d = (h=((e=a++) + (f=y++))) - (g=++y) = 89

h = 103

e = 91

f = 12

a = 92

y = 14

g = 14

From the walkthrough...

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 ***

Henry

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.

Nope. No stupid mistakes. All the interim values (dozen variables) match the interim values calculated in the walkthrough. And I don't see anything strange happening. It looks fine.

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.

I am not sure what you mean by this. Can you elaborate a bit? Or are you confusion the interim values to be the interim values of x and y?

Henry

I don't think there is anything strange happening in your program and you did not make any mistakes either.

In the first expression, x = (x++) + (x++), x is not being inremented BEFORE, it is being incremented AFTER it is used.

So, x = (45)

*(still 45 is being used, even though x is already 46)*+ ( 46 ++)

which is (45) + (46)

*(And here, 46 is being used, even though x is incremented to 47)*, which is 91. You never use the incremented value in x++, you always use the value before it was incremented.