programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Is there a URL that explains[ y = x++ + ++x; ] ?

Ranch Hand
Posts: 206
• Number of slices to send:
Optional 'thank-you' note:
Is there a URL that explains[ y = x++ + ++x; ] ?

Sheriff
Posts: 67745
173
• Number of slices to send:
Optional 'thank-you' note:
www.coderanch.com

Bear Bibeault
Sheriff
Posts: 67745
173
• Number of slices to send:
Optional 'thank-you' note:
In other words, you could ask here. After all, there isn't going to be a URL for each permutation of a Java expression.

Harry Henriques
Ranch Hand
Posts: 206
• Number of slices to send:
Optional 'thank-you' note:
Hi,

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

Ranch Hand
Posts: 257
• Number of slices to send:
Optional 'thank-you' note:
Hey Harry,

As we know that + and -has its associatevity from left to right . I have put the numbers explaining how this is calculated.

I hope this would clear your confusion.

Sheriff
Posts: 9707
43
• Number of slices to send:
Optional 'thank-you' note:
See if this FAQ helps...

Ranch Hand
Posts: 3852
• Number of slices to send:
Optional 'thank-you' note:
Try wolframalpha.com. It claims to know everything that is computable.

Harry Henriques
Ranch Hand
Posts: 206
• Number of slices to send:
Optional 'thank-you' note:

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

Harry Henriques
Ranch Hand
Posts: 206
• Number of slices to send:
Optional 'thank-you' note:
Hi, I could use some help on this one. I thought that I had this issue figured out. I thought that it was strictly a matter of Operator precedence and left to right Operator associativity when calculating the rvalue. But, that is not the case. There is a more complex mechanism that is operational in these type of problems. I can't figure out what that mechanism is. Can you give me a hand? Look at the attached code block. A comparison with the code block in the preceding post will make the problem clear.

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

Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
Harry,

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.

author
Posts: 23949
142
• Number of slices to send:
Optional 'thank-you' note:

these two numbers should agree. the correct answer is 88, not 90.

I don't understand the "should agree" part -- these look like two different expressions.

Henry

Henry Wong
author
Posts: 23949
142
• Number of slices to send:
Optional 'thank-you' note:

To walk through the "basic problem"...

int x = 45;
int y = 12;

--> x is now 45 and y is now 12

x = x++ + x++;

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;

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 Henriques
Ranch Hand
Posts: 206
• Number of slices to send:
Optional 'thank-you' note:
Henry, I don't see anything wrong with the logic of your previous post, but your post doesn't answer my question. I have listed two examples below that illustrate the problem. In the first example, "x" is post-incremented before the addition operation is effective. In the second example "x" and "y" are added together first, then "x" and "y" each are post-incremented. Do you see what I mean?

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 Wong
author
Posts: 23949
142
• Number of slices to send:
Optional 'thank-you' note:

Do you see what I mean?

No... Can you simplify your question? Your example has about a dozen variables being changed in all sort of ways.

Can I use this as the simplified example?

And BTW, what is the question?

Henry

Henry Wong
author
Posts: 23949
142
• Number of slices to send:
Optional 'thank-you' note:
Actually, in reading your example a bit more, it looks like all the other letters are just the interim stuff, and it matches everything in my walkthrough. What is it about the walk through that didn't answer your question? If you agree with it, shouldn't it apply to your program?

Henry

Henry Wong
author
Posts: 23949
142
• Number of slices to send:
Optional 'thank-you' note:

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++;

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;

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

Harry Henriques
Ranch Hand
Posts: 206
• Number of slices to send:
Optional 'thank-you' note:
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

Henry Wong
author
Posts: 23949
142
• Number of slices to send:
Optional 'thank-you' note:

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++;

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

Henry Wong
author
Posts: 23949
142
• Number of slices to send:
Optional 'thank-you' note:

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;

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
Ranch Hand
Posts: 206
• Number of slices to send:
Optional 'thank-you' note:
Thanks. It is just very hard for me to predict the order of execution. I'll have to think about it some more.

Thanks.
Harry

Henry Wong
author
Posts: 23949
142
• Number of slices to send:
Optional 'thank-you' note:

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

Bartender
Posts: 3648
16
• Number of slices to send:
Optional 'thank-you' note:
I don't get how come a simple question can turn into a series of responses. Let say both y=0 and x=0 to begin with.

Ranch Hand
Posts: 537
• Number of slices to send:
Optional 'thank-you' note:
SCJP6 doesn't focus on precedence operators much and certainly not on questions like x++ + ++y. I guess its said in the K&B guide also. Well about the mock may be its an older version that you have. Well any ways, just keep the prefix and postfix operator concepts and how the value is given to the expression by the operators.

Uma Bandaru
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
Harry,

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.