+ 9
What is the difference between i++ and ++i?
Description
15 Answers
+ 8
This should be rejoiced as the new meme of 2017, and the difference is bagels
+ 8
if you are for serious that you do not understand..
++i = add 1 before expression is evaluated.
i++ = add 1 after expression is evaluated.
+ 7
ur gay and c++ is not
+ 6
@jay OF COURSE I'M SERIOUS! DO YOU MEAN PEOPLE HERE AREN'T SERIOUS? ANYWAY I STILL DON'T GET IT, THEY BOTH DO THE SAME THING
+ 6
ANSWER ME JAY!
+ 5
đ settle..I wasnt the one saying silly things to you... anyway. they do not do the same thing. that is why there is two versions.
int i = 5;
cout << ++i; // 6
cout << i++; // 6
cout << i; // 7
+ 5
i think its called b
+ 4
lowercase i and uppercase I made the difference
+ 4
@David Sebastian Keshvi Illiakis Thanks for making me notice, but still, what's the difference? I don't get it!
+ 4
im not italian so i dont speak italics
+ 4
@jay YOU LIED! IT'S NOT WORKING! WHAT'S THE REAL DIFFERENCE? IS I++ A REFERENCE TO C++? IS THERE A CODING LANGUAGE CALLED ++C THEN?
+ 3
++i : Operator used here is called pre-increment operator. It increments the value of the variable before the other arithmetic operations are done.
i++ : Operator used here is called post-increment operator. It increments the value of the variable after all other operations have been done and the statement is about to terminate.
When used independently, ie without any other operation or assignment, both produce same results. But, when other operations are done along with increment, the result may change.
eg, when used in a 'for' loop for the updating of the variable used, it produces same result with pre or post increment operators.
But when used as assignment it produces different results.
int i=1.
int j=++i;
cout<<i<<j<<endl; //22
int j=i++
cout<<i<<j; //32
+ 2
++i is preincrement
i++ is postincrement
+ 1
Here is the difference:
i++ is evaluated before increment (increase by 1).
++i is increment before evaluated.
+ 1
++i --> increment i by one and return the new value
i++ --> increment i by one and return the value it was before the increment
In principle, ++i is more efficient, because i++ has to store the original value and return it after incrementing i.
However, modern compilers detect when it doesn't matter, which is when the returned value of the increment operator is not used (e.g. in those infamous for loops), and optimize it so you won't get penalty for using post increment (which is the style most developers prefer for some reason).