• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does prefix and postfix change the answer?  RSS feed

 
Cj Hooper
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Will the postfix x++ and the prefix --y change the answer for this question?



If x has the value 10 and so does y, then what is the value of (x ++) * (-- y)?

Answer: 11*9

Thank you so much!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly sure what you're looking for here. Is that "answer" your answer? Is that what you think the expression evaluates to?

The best way to figure this out is to put it in a program and run it. Then you can see if what you think should happen actually does happen.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prefix == apply the increment/decrement and then use the value
Postfix == use the value and then apply the increment/decrement

I don't think the answer to (x++) + (--y) is 11 * 9
Yes x ends with a value of 11, and Y ends with a value of 9
But those wouldn't be the values used in the expression.
 
Cj Hooper
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both! When I run the program, I receive the output 10*9. The problem doesn't specify after the program is executed and that is what is throwing me off. The answer is 11*9 according the quiz. This is the coding I ran to test:

 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with prefix the ++ or -- happens before the operation, and with postfix after it.

int a = 1;

b = --a;
//a and b both zero at this line

a = 1;
b = a--;
//b is 1 and a is 0 at this line.

postfix is more traditional probablybecause it works well incrementing a zero based index.
Be careful in the last field of a for loop, because the are both the same! Either way the ++ or -- happens at the bottom of the loop.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cj Hooper wrote:Thank you both! When I run the program, I receive the output 10*9. The problem doesn't specify after the program is executed and that is what is throwing me off. The answer is 11*9 according the quiz. This is the coding I ran to test:

Well, the answer according to the quiz is wrong and you can take that to your instructor. Here's a variation of your test:

Edit: BTW, the output of the above is
 
Ash Jon
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are a lot easier to understand than you think they are, they just look a bit daunting...

I always found it best described using for-loops:



Hope it helps
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think this is actually easy to understand. It causes no end of confusion to beginners. The problem is that there are two values to consider in i++.
There is the value of i and there is the value of the whole expression. The value of the whole expression i++ is the same as the old value of i. We have an FAQ about that.
The same applies to ++i but when the value of ++i is equal to the new value of i, most people find that easy to understand.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, Ash, but your description regarding the for-statement and prefix/postfix is incorrect.
Ash Jon wrote:
I always found it best described using for-loops:


This

produces the output

As you can see, the output from both loops are identical. The use of the postfix operator on the index of a for-statement is idiomatic. That is to say, that's just how most people normally write it. Using a prefix increment is similar to saying it how Yoda, the character from Star Wars, would: "incremented i is, hmmm" instead of the normal way, "i is incremented." Semantically, however, there is no difference between i++ and ++i as the increment expression of the for-statement. This is because, contrary to what you described, the increment is always executed after the body of the loop is executed.

From https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html:
The increment expression is invoked after each iteration through the loop;


The rest of your example where postfix/prefix is used in an assignment statement is correct though.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote:The rest of your example where postfix/prefix is used in an assignment statement is correct though.

Except maybe for the second part, which might confuse people if they think that execution continues from the first part:

Your comment on line 8 can be misleading because it's based on the assumption that the variable j is still 2. However, j is already 3 after line 4, in which case line 9 would print 5, not 4 as your comment implies.

It might be clearer if we presented it this way instead:
 
Ash Jon
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the corrections Junilu. I seriously did not know this was the case with for-loops...
I assumed it was the case, as I've never had to increment prior to execution of the body before (How silly do I look!)

Regarding the second part. I had meant to change j back to 2, as I did with with i (i = 1).

Thanks! Interesting to know that It's appreciated.

Ash
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a problem, Ash. you should see how silly I look sometimes. But it's all good, we're all learners, even some of the old dogs around here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!