Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between ++Var and Var++ [duplicate]

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) {} 
like image 855
user559142 Avatar asked May 30 '11 10:05

user559142


People also ask

What is the difference of using VAR and not using var in REPL?

What is the difference of using var and not using var in REPL while dealing with variables? Use variables to store values and print later. if var keyword is not used then value is stored in the variable and printed. Whereas if var keyword is used then value is stored but not printed.

What is the difference between declaring a variable with the keyword var and without the keyword var?

Variables declared outside a function become GLOBAL, and all scripts and functions on the web page can access it. Global variables are destroyed when you close the page. If you declare a variable, without using "var", the variable always becomes GLOBAL.

What is difference between Val and VAR keywords?

The keywords var and val both are used to assign memory to variables at the running only. The difference is in the mutability of variables that are initialized using these keywords. var keyword initializes variables that are mutable, and the val keyword initializes variables that are immutable.


2 Answers

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.

like image 142
Dónal Avatar answered Oct 11 '22 18:10

Dónal


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.

like image 34
phoxis Avatar answered Oct 11 '22 17:10

phoxis