Thread: i++ and ++i
View Single Post
Aug 14th, 2004
0

Re: i++ and ++i

Quote originally posted by cscgal ...
Can someone please explain to me the difference between ++i and i++ when written as part of an expression (i.e. within loops and if statements) ? Thanks
//Lets declare i, and set it equal to 4.
int i = 4;

//Now say their is a function nammed goFetch(int), and we wanted to pass an increment of i to it

//i = 4 before this code gets touched, the value 4 would be passed, and then i would become 5
goFetch(i++);

//i = 4 before this code gets touched, i gets incrimented before anything else (and becomes 5), and the value 5 would be passed
goFetch(++i);

So, ++i means to incriment first then give the incrimented value, and i++ means to give the original value, then incriment.
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002