The difference between ++Var and Var++ [duplicate]
Asked Answered
O

8

118

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) {}
Osage answered 30/5, 2011 at 10:50 Comment(2)
#484962Charin
Two of the answers provided in this thread were 100% wrong and have been deleted. The accepted answer in #484962 is a little stupid, but it's not actually incorrect. The answer with 26 upvotes in that question, however, explains everything you've asked.Charin
K
178

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.

Kirby answered 30/5, 2011 at 10:53 Comment(0)
B
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.

Blither answered 30/5, 2011 at 10:55 Comment(0)
A
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.

Adel answered 30/5, 2011 at 10:58 Comment(0)
W
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
Whittington answered 30/5, 2011 at 10:57 Comment(0)
P
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.

Palatinate answered 30/5, 2011 at 10:54 Comment(0)
B
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
Brahmin answered 20/1, 2018 at 15:8 Comment(0)
C
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

Cheek answered 30/5, 2011 at 10:55 Comment(3)
Why would that code snippet ever try to write to data[16]?Pogy
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.Cheek
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. 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!Reimpression
H
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;
Hardpressed answered 30/5, 2011 at 10:57 Comment(1)
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.Reimpression

© 2022 - 2024 — McMap. All rights reserved.