116

In programming, particularly in Java, what is the difference between:

int var = 0;
var++;

and

int var = 0;
++var;

What repercussions would this have on a for loop?

e.g.

for (int i = 0; i < 10; i++) {}

for (int i = 0; i < 10; ++i) {}
Boann
  • 48,794
  • 16
  • 117
  • 146
user559142
  • 12,279
  • 49
  • 116
  • 179

8 Answers8

176

tldr;

Although both var++ and ++var increment the variable they are applied to, the result returned by var++ is the value of the variable before incrementing, whereas the result returned by ++var is the value of the variable after the increment is applied.

Further Explanation

When ++var or var++ form a complete statement (as in your examples) there is no difference between the two. For example the following

int x = 6;
++x;
assert x == 7;

is identical to

int x = 6;
x++;
assert x == 7;

However, when ++var or var++ are used as part of a larger statement, the two may not be equivalent. For example, the following assertion passes

int x = 6;
assert ++x == 7;

whereas this one fails

int x = 6;
assert x++ == 7;

Although both var++ and ++var increment the variable they are applied to, the result returned by var++ is the value of the variable before incrementing, whereas the result returned by ++var is the value of the variable after the increment is applied.

When used in a for loop, there is no difference between the two because the incrementation of the variable does not form part of a larger statement. It may not appear this way, because there is other code on the same line of the source file. But if you look closely, you'll see there is a ; immediately before the increment and nothing afterwards, so the increment operator does not form part of a larger statement.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
Dónal
  • 185,044
  • 174
  • 569
  • 824
18

int a = 5, b;

post increment : b = a++; : a is first transferred to b and then a is incremented, so now b is 5, and a is 6 The effect is b = a; a = a + 1;

pre increment: b = ++a; : first a is incremented and then the result is transferred into b, so now a is 7 and also b is 7. The effect is a = a + 1; b = a

a++ and ++a staying independently act in the similar way. In the loop examples you have presented, the increment operators is not associated in any expression, and are independent. Therefore these two in this particular implementation is identical.

phoxis
  • 60,131
  • 14
  • 81
  • 117
15

++var is the pre-increment operator; it increments the value of var before evaluating the expression. Similarly, var++ is the post-increment operator; it increments the value of var after evaluating the expression.

In the case of a simple loop, there is no difference between two, because the expressions ++var; and var++; both yield to the same result.

For more info, see for example http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter02/operators.html#IncDecOps.

Eser Aygün
  • 7,794
  • 1
  • 20
  • 30
13

var++ returns its value before incrementing.
++var returns its value after incrementing.

int var = 0;
System.out.println(var++); // returns 0;

var = 0;
System.out.println(++var); // returns 1
Kenny Linsky
  • 1,726
  • 3
  • 17
  • 41
Patryk Dobrowolski
  • 1,565
  • 2
  • 13
  • 29
7

In your examples, there is no difference, there is however a difference between:

int var = 0;
int var2 = ++var;

and:

int var = 0;
int var2 = var++;

In the first case, the value of var2 is 1 while in the second, it's 0.

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
1

Both ++var and var++ are identical when appear in expression alone. This applies to your question because you have alone ++i, i++

It difference only take place when you inline them:

int x = 0;
printf( "%d %d\n", ++x, x ); // 1 1
printf( "%d %d\n", x++, x ); // 1 2

How to remember?

When you see first the operator, then increment and later take value.

When you see first the variable, then take value and later increment.

So in first example you see equal values because:

you increment `x`, then access `x`, then access `x` again  

So in second example you see differences because:

you access `x`, then increment `x`, then access `x` again
Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
0

++i is preincrement, it is done before anything in the expression it appears.

i++ is postdecrement, it is done after anything in the expression it appears.

in the first loop, you'll run from 0 to 9. In the second, it will run from 1 to 9.

My advice: avoid both of them (exceptions may be i++; or the first loop). Too tricky to look for in the expression.

I passed a long day debugging

myArray[(i++)%16] = data

and trying to find why it did try to write to data[16] sometimes

SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • 1
    Why would that code snippet ever try to write to `data[16]`? – Tom Shaw May 30 '11 at 10:59
  • Ok my failure... it did end with i == 16 but at that point it wrote to data[15], the error happened later because I thought i < 16 (because I had put the i++ in brackets). This code was written ten years ago so I do not remember the exact way it crashed. I remember clearly the time spent looking for it and my firm resolution to never use i++ or ++i in the middle of an expression again. – SJuan76 May 30 '11 at 11:32
  • not correct at all! Postincrement is NOT executed after anything in the expression, actually it has the highest precedence of all operators, that is, it is executed first: [Operators](http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html). The difference it just which value is returned by the increment: the value before or the one after incrementing. Also both loops work do exactly the same; they are compiled to the identical byte code! – user85421 May 30 '11 at 12:31
0

In fact, this is rather simple. The preincrement ++i is executed first in the statement.

j = ++i +1;

is equivalent to

i = i+1;
j = i+1;

whereas the post increment i++ is executed at the end;

j = i++ +1;

is equivalent to

j = i+1;
i = i+1;
Eilistraee
  • 8,220
  • 1
  • 27
  • 30
  • not correct. In `j = i++ +1` the increment is not executed at the end. The addition is still executed last (before the assignment). The value returned by `i++` is the value of `i` **before** incrementing; as opposed to `++i` which returns the value **after** incrementing. – user85421 May 30 '11 at 12:17