-2

This question is from SCJP

int x = 0; 
int y = 10; 

do {
    y--;        
    ++x; 
} while (x < 5); 

System.out.print(x + "," + y);

What is the result?

A. 5,6

B. 5,5

C. 6,5

D. 6,6

ANSWER is B

but i was wondering why its not A. because we know that after the post-decrement(y--) the value remains same as before the decrement i.e 6. Please provide a reason

Thanks in advance

Tiny
  • 27,221
  • 105
  • 339
  • 599
user2985842
  • 437
  • 9
  • 24
  • possible duplicate of [Is there a difference between x++ and ++x in java?](http://stackoverflow.com/questions/1094872/is-there-a-difference-between-x-and-x-in-java) – devnull Mar 06 '14 at 17:26
  • I don't think the question has anything to do with post or pre increments or decrements, just follow the logic through. The increments and decrements are on their own line, so they'll just happen irrespective of any other lines of code here. – Dan Temple Mar 06 '14 at 17:27
  • You can use `--y` and `x++` and still get the same answer here – Chaos Mar 06 '14 at 17:27

4 Answers4

3

Post and pre-decrement actually both decrease the value of your variable after that line has been executed. Say we have this:

int x = 10, y = 10;
x++; //After this line, x = 11
++y; //After this line, y = 11

System.out.println("x = " + x + " y = " + y);

With that, we'd get x = 11 y = 11 as output.

The Difference:
The difference between post and pre decrement is that with one, the post decrement, the actual decrement occurs after the variable is evaluated. So if we had the following:

int x = 10;
System.out.println("x = " + x--);

We would get x = 10 outputted.

With pre-decrement, the actual decrement occurs before the variable is evaluated. So if we had the following:

int x = 10;
System.out.println("x = " + --x);

We would get x = 9 outputted.

Bryan Muscedere
  • 572
  • 2
  • 10
  • 23
1

Yes, the value of the expression y-- is the old value, but that value is thrown away immediately. The value of y is already changed down to 5 by the time it's printed after the loop has ended.

Last iteration of the loop.

y--;  // Value is 6, but y is changed to 5.
++x;  // Value is 5, x is changed to 5.

After the loop ends, both x and y are already 5. Because you're not doing anything with the expressions, it actually doesn't matter whether you're using pre-increments/decrements or post-increments/decrements here. The result is the same in that the values are updated well before they're referenced in the System.out.print statement.

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

Follow the code:

1 Loop: y = 9, x = 1
2 Loop: y = 8, x = 2
3 Loop: y = 7, x = 3
4 Loop: y = 6, x = 4
5 Loop: y = 5, x = 5

That's it. In the 5 Loop x = 5 so the do while breaks.

Julian Ezequiel
  • 183
  • 1
  • 10
0

The value of y-- does not remain the same. The only diference between pre and post inc/decrement is the value of the evaluated expression, so:

int y = 0;
int a = ++y; //a = 1, y = 1
int b = y--; //b = 1, y = 0

So, following the loop now you will see why B.

suderio
  • 56
  • 1
  • 4