Thread: i++ and ++i
View Single Post
Join Date: Feb 2002
Posts: 898
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Solved Threads: 27
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: i++ and ++i

 
0
  #2
Aug 14th, 2004
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.
-Ryan Hoffman

.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote