Difference between postfix and prefix increments?

May 29, 2015 at 5:57pm
closed account (yR9wb7Xj)
I always seem to get confuse what is the difference between postfix increments and prefix and how to use them. To be more specifically when do you know you need to use postfix or prefix increments. Can you please provide an example, I have read in the tutorials for it, but I want a deeper understanding on it, thank guys.
May 29, 2015 at 6:01pm
it's just that, at post increment you get the value before you incremented and with pre-increment you get the value after you incremented.

pre-increment is a tiny little bit faster because you don't have to store the previous value

1
2
3
4
5
6
7
8
int main()
{
    int a = 5;
    int b = ++a; // b = 6 | a = 6
    int c = a++; // c = 6 | a = 7

    return 0;
}
May 29, 2015 at 6:14pm
Guideline: use the pre by default.
May 29, 2015 at 6:27pm
closed account (yR9wb7Xj)
Ok, I understand it I think. So originally int (a) was store as 5. From there store a prefix a for int (b). meaning it it's going to increment int (a) to 6 and keep the same value store for (b).
int (c) - ->( a) is store as postfix increment, so the original value of 5 after it was incremented by (b) it's now 6 for the value of (a), and now( a) is incrementing it one more to 7, and (c) is being increment from the original value of (a) to 6 correct?



And Keskiverto, thank you for that tip I wil keep that in mind.
Last edited on May 29, 2015 at 6:30pm
May 29, 2015 at 6:32pm
Not quite correct. When a = 6, the value of a++ is 6, and yet if it was then checked again, it would be 7. The point is that a++ returns the value of a, then increments it, but ++a increments a, and then returns the value of a.
May 29, 2015 at 6:40pm
closed account (yR9wb7Xj)
Thank you shadowmouse for clarifying that. I understand it now. So you're saying that prefix increments ++a is incremented to the original store for int a right? And then it returns that value the same for int b? Correct? And for int c the postfix increment a++ is being increment after the value original value of was increment by b to 6 now it's being increment 1 more to 7, and c is being incremented by the original value from 5 to 6 correct?
May 29, 2015 at 6:48pm
I'm not entirely sure if that's correct but it might well be, but just in case, here are a couple of function that replicate the operators.
1
2
3
4
5
6
7
8
9
10
11
12
int prefix(int & operand)
{
  operand = operand + 1;
  return operand;
}

int postfix(int & operand)
{
  int tempOperand(operand);
  operand = operand + 1;
  return tempOperand;
}

This means the example would be rewritten as follows
1
2
3
4
5
6
7
8
9
int main()
{
  int a = 5;
  int b = prefix(a); //This is equivalent to a=a+1; b=a;
  int c = postfix(a); // This is equivalent to c=a; a=a+1;
  //a == 7, b==6, c==6
  //The increment operators have no effect on b or c themselves, but rather they affect a
  //before or after b and c have their value set
}

You might well have got it last time, but I wasn't sure and I have some spare time so code! It just sounded to me as if you were saying the the increment operators were actually incrementing b and c themselves.
Last edited on May 29, 2015 at 6:51pm
May 29, 2015 at 7:18pm
closed account (yR9wb7Xj)
Ok I understand now. Thank you for this. Yeah, I thought it did that, but now I know it does not.
Topic archived. No new replies allowed.