What is the difference between i++ and ++i? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 9

What is the difference between i++ and ++i?

Description

13th Aug 2017, 8:29 AM
Dao
Dao - avatar
15 Respostas
+ 8
This should be rejoiced as the new meme of 2017, and the difference is bagels
13th Aug 2017, 8:36 AM
SoraKatadzuma
SoraKatadzuma - avatar
+ 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.
13th Aug 2017, 8:44 AM
jay
jay - avatar
+ 7
ur gay and c++ is not
13th Aug 2017, 8:34 AM
EmptyBox
EmptyBox - avatar
+ 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
13th Aug 2017, 8:51 AM
Dao
Dao - avatar
+ 6
ANSWER ME JAY!
13th Aug 2017, 9:12 AM
Dao
Dao - avatar
+ 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
13th Aug 2017, 8:56 AM
jay
jay - avatar
+ 5
i think its called b
13th Aug 2017, 9:12 AM
jay
jay - avatar
+ 4
lowercase i and uppercase I made the difference
13th Aug 2017, 8:31 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 4
@David Sebastian Keshvi Illiakis Thanks for making me notice, but still, what's the difference? I don't get it!
13th Aug 2017, 8:36 AM
Dao
Dao - avatar
+ 4
im not italian so i dont speak italics
13th Aug 2017, 8:37 AM
EmptyBox
EmptyBox - avatar
+ 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?
13th Aug 2017, 9:05 AM
Dao
Dao - avatar
+ 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
13th Aug 2017, 4:51 PM
Harsh Kumar
Harsh Kumar - avatar
+ 2
++i is preincrement i++ is postincrement
14th Aug 2017, 3:56 PM
Pankaj Bairwa
Pankaj Bairwa - avatar
+ 1
Here is the difference: i++ is evaluated before increment (increase by 1). ++i is increment before evaluated.
13th Aug 2017, 9:36 AM
Derek Leiro
Derek Leiro - avatar
+ 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).
13th Aug 2017, 11:28 AM
Sivan Tal
Sivan Tal - avatar