After watching Precedence in the Week 1 Shorts, I know what i++ means, but I still don't understand: What is the difference is between i++ and ++i?

2 Answers 2


x++ - Post-increment operator
++x - Pre-increment operator

In short,
x++ - x is incremented after the current statement is evaluated.
++x - x is incremented before the current statement is evaluated.

Well, lets say we have a function called foo which has a local variable x:

int foo()
    int x = 0;
    return x++;

In this function, x is initialized to 0 and foo returns x++.

At first you might think that the return value of foo will be 1, since x = x + 1 (i.e. x++) is equal to 1.

However, recall that for x++, x is incremented after the current statement is evaluated. So the current value of x (0) is returned first, and then x is increment (in actual fact, since the statement is leaving the function, x disappears).

This is in contrast to ++x, which updates the value of x first, before it's used in the current statement.

An easier way to think about this is this:

int y = x++; // x is zero in this statement until the next line, so y is 0
int z = ++x; // x is now one from the last statement, but is increment by 1 again before assigned to z, so z is 2

Short, simple answers are often useful. Here's mine:

  • ++x adds one to X before the current statement
  • x++ adds one to X after the current statement

I hope that helps :)

  • 0 down vote I found the long answer to be more concise for an explanation. Rather than just memorizing the code, understanding it makes it easier to remember. I guess it's all in the learning curve :)
    – Jennifer
    Sep 8, 2014 at 17:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .