Results 1 to 10 of 10
 09132011, 04:15 AM #1Member
 Join Date
 Sep 2011
 Posts
 3
 Rep Power
 0
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?
 09132011, 04:23 AM #2
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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.
Java Code:public class Increment{ int a; public int increment(int a){ int x = a; a = a+1; return x; } }
 09132011, 04:28 AM #3Member
 Join Date
 Sep 2011
 Posts
 12
 Rep Power
 0
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++
 09132011, 04:42 AM #4
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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
Java Code:a++; int x = a; x; int c = x c++ System.out.println(x + ", " + a + ", " + c);
Java Code:int x = ++a; int c = x; System.out.println(x + ", " + a + ", " + ++c);
 09132011, 05:06 AM #5Member
 Join Date
 Sep 2011
 Posts
 3
 Rep Power
 0
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?
 09132011, 05:57 AM #6
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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
Java Code:int a=b=5; System.out.println("postfix: " + a++); System.out.println("prefix: " + ++a);
 09132011, 06:03 AM #7Member
 Join Date
 Sep 2011
 Posts
 3
 Rep Power
 0
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.
 09132011, 06:03 AM #8Member
 Join Date
 Sep 2011
 Posts
 23
 Rep Power
 0
Re: Looking for explanation of equation
I am no expert but i ran your code and i got a=16; b=15 just fine?
Java Code:int a = 20; int b = 10; a = (a + b) / 2; b = a++; System.out.println(a); System.out.println(b);
16
15
BUILD SUCCESSFUL (total time: 1 second)
 09132011, 06:06 AM #9
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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
Java Code:a=a+1;
 09132011, 06:08 AM #10Member
 Join Date
 Sep 2011
 Posts
 23
 Rep Power
 0
Similar Threads

Help with equation
By _Jk_ in forum New To JavaReplies: 10Last Post: 03172011, 09:15 PM 
equation
By bobo67 in forum New To JavaReplies: 5Last Post: 09062010, 07:40 PM 
Need help with math equation
By annabellastorm in forum New To JavaReplies: 4Last Post: 01102010, 06:12 PM 
differential equation RK4
By arvindmer in forum New To JavaReplies: 3Last Post: 01082009, 02:27 PM 
Quadratic Equation
By jpnym15 in forum New To JavaReplies: 4Last Post: 11122008, 04:29 AM
Bookmarks