• Post Reply Bookmark Topic Watch Topic
  • New Topic

postfix vs prefix  RSS feed

 
Trent Davies
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I was just testing out this piece of code and I was expecting the console to print out 20 for the variable, value and 16 for the variable, number.
I got 15 and 25 instead. I would appreciate if someone can help me figure out why this is the case.
Thank you!


 
Tim Holloway
Bartender
Posts: 18716
71
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prefix and postfix expressions have side-effects. Most of the time, these side effects can be tricky enough that you're best off using them only in simple expressions. Your example is a case in point.

The expression "int value = j * i++;" is the logical equivalent of "value = j * i; i = i+1". It's a postfix increment, which means that FIRST you return the value of I as it was (3) and THEN you increment (update) i itself (i = 4).

The expression "int number = ++i * j;" if a PREFIX increment, so its equivalent is "i = i+1; number = i * j". You increment i again (from 4 to 5), then multiply the updated value of i by j. Thus, 5 * 5 = 25.
 
Campbell Ritchie
Marshal
Posts: 55789
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Trent Davies
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim for the help.

I'm still wondering why you increment i again from 4 to 5 for the expression int number = ++i * j; ?

i = i + 1; //brings i from 3 to 4.

number = i * j; // shouldn't number be 4 * 5 = 20?

 
Tim Holloway
Bartender
Posts: 18716
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"number = i * j" would indeed be 4x5=20.

But the expression was "number = ++i * j", so first i would be incremented, then the updated value of i is used, resulting in 5x5=25.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be more specific, AFTER you compute value, i is now equal to 4. then when you compute number, you increment i again, and since it is a pre-increment, it becomes 5 before the multiply is preformed, to get 25 as the result.
 
Vaibhav Sagar
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Trent, I think you have two misunderstandings based on your question.
1. Between post and pre increment operator
2. What happens to the variable once its post/pre incremented.

Lemme break it down for you-
int i = 3;
int j = 5;

int value = j * i++;
----- What you are expecting = (j*i)+1
= (5*3)+1
= (15)+1
= 16
----- What actually happens = (j*i) and i++
= (5*3) and i=3+1
= 15, and i is incremented to 4 only after j * i is done

int number = ++i * j;
----- What you are expecting = (1+i) * j
= (1+3) * 5
= (4)*5
= 20
----- What actually happens = (1+i) * j
= (1+4) * j, i was set to 4 in the previous line using the post increment operator and is no longer equal to 3
= (5)*5
= 25

I think it would be good if you read about post/pre increment/decrement operators again and try out some code with System.out.println statements to see how the value gets increased after the operations.
 
Liutauras Vilda
Marshal
Posts: 4665
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trent Davies, welcome to the Ranch
Vaibhav Sagar, welcome too (by the way, have a cow for the putted effort in your answer)

Extra readings - read carefully this thread.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trent Davies wrote:I was just testing out this piece of code and I was expecting the console to print out 20 for the variable, value and 16 for the variable, number.
I got 15 and 25 instead. I would appreciate if someone can help me figure out why this is the case.

Hi Trent, and welcome to JavaRanch.

I think everybody else has covered your main question, but the reason you're having problems to begin with is that combining increment operators with expressions is a really bad idea, so I'd get out of the habit of doing it as soon as you've taken whatever exam you're studying this for.

Winston
 
Tim Holloway
Bartender
Posts: 18716
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
....combining increment operators with expressions is a really bad idea, so I'd get out of the habit of doing it as soon as you've taken whatever exam you're studying this for.

Winston


One reason I've never been big on cert exams. When people dump code like that on me in real life, my first reaction is to (ahem) gently suggest that Bad Things will happen to them if they keep doing that. I don't have the patience to want to unsnarl it.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!