Looking for explanation of equation

• 09-13-2011, 04:15 AM
flyersjoe
Looking for explanation of equation
I cannot figure out the equation below:
int a = 20;
int b = 10;
a = (a + b) / 2;
b = a++;

The correct answer is (a=16; b=15)
However, I come out to (a=15; b=16). I've been trying to figure out what I'm missing here; can anyone offer a pseudocode breakdown of how this equation results in a=16, b=15?
• 09-13-2011, 04:23 AM
sunde887
Re: Looking for explanation of equation
The postfix operator works different than you may expect. It returns the value it currently is, and then increments the value.

Code:

```public class Increment{   int a;   public int increment(int a){     int x = a;     a = a+1;     return x;   } }```
This is how it would look. Notice that it returns a's original value, and then increments a. ++a(prefix) will first increment a, then return it.
• 09-13-2011, 04:28 AM
popeus
Re: Looking for explanation of equation
yeah the way I was taught it was that incrementations are done after setting the variable,
ie
it does
b=a
a++
• 09-13-2011, 04:42 AM
sunde887
Re: Looking for explanation of equation
As popeus pointed out; you can avoid a great amount of confusion by simply putting increments on a seperate line. If you don't truly understand them, avoid getting fancy with them. It's much easier to read something like

Code:

```a++; int x = a; x--; int c = x c++ System.out.println(x + ", " + a + ", " + c);```
As opposed to
Code:

```int x = ++a; int c = --x; System.out.println(x + ", " + a + ", " + ++c);```
• 09-13-2011, 05:06 AM
flyersjoe
Re: Looking for explanation of equation
Thank you for your responses; I'm embarrassed to say however, that I didn't understand them. This is my second week in my Java class...I've been cruising until I hit this equation. This is how I am logically breaking it down...tell me where I'm wrong.
int a = 20;
int b = 10;
a = (a + b) / 2;
b = a++;

new variable "a" is given a value of 20;
new variable "b" is given a value of 10;
variable a is assigned a new value of (20+10) divided then by 2 = 15.
So now, "a" has the value of 15.
variable "b" is then assigned a new value, variable "a" increased by 1....so 15 + 1 = 16
End of my logic.
So I can't figure how "b" = 15 and how "a" gets assigned the value of 16.
Taking a look at my logic, can you tell me where I'm going wrong?
• 09-13-2011, 05:57 AM
sunde887
Re: Looking for explanation of equation
The increment operator '++' has a side effect. This side effect chagezthevariable it's applied to. If you put 'a++' on it's own line a would be increased by 1(it's value is changed) depending on whether it is postfix 'a++' or prefix '++a' the order of operations is different. Postfix will show the value of a before it's changed, and then change a. The prefix operator changes it and then returns the changed variable.

Your error comes at the following point: 'b=a++' this does two things; first it assigns a to b, so since a is 15, b becomes 15, then it increments a, changing a to 16.

Try this
Code:

```int a=b=5; System.out.println("postfix: " + a++); System.out.println("prefix:  " + ++a);```
I'm hoping this may help you understand the difference between postfix and prefix upon running it.
• 09-13-2011, 06:03 AM
flyersjoe
Re: Looking for explanation of equation
Thank you very much...I didn't realize the a++ could reassign a value to the variable "a". I'm going to readu of the postfix and prefix operators... that should get me back on track.
Thanks again... good stuff.
• 09-13-2011, 06:03 AM
Jason
Re: Looking for explanation of equation
I am no expert but i ran your code and i got a=16; b=15 just fine?

Code:

```        int a = 20;                 int b = 10;               a = (a + b) / 2;               b = a++;         System.out.println(a);         System.out.println(b);```
output:
16
15
BUILD SUCCESSFUL (total time: 1 second)
• 09-13-2011, 06:06 AM
sunde887
Re: Looking for explanation of equation
His problem was that when evaluating it in his head he got a=15,b=16

@op: postfix increment is the same as
Code:

`a=a+1;`
• 09-13-2011, 06:08 AM
Jason
Re: Looking for explanation of equation
Ahhh i see