i++ vs ++i

1. JPP Guest

## i++ vs ++i

I&#039;m reading all my books concerning for loops, and they all have the example like for(i=0;i&#060;10;i++)<BR>My boss wants me to change it to ++i because he says its more efficient. I know the difference between the two, but how can you tell which situation to use what?<BR><BR>TIA,<BR>JPP

2. Member
Join Date
Dec 1969
Posts
44

## RE: i++ vs ++i

Â*<BR>To my knowledge,<BR><BR>i++ increments after the number is calculated and ++i increments before the number is calculated as part of an expression.<BR><BR>But when i is by itself, this doesn&#039;t matter as i is not part of a system of operations like :<BR>__________________________<BR>var i = 2;<BR>var j = 3 * ++i // j = 9 and i = 3;<BR><BR>var j = 3 * i++ // j = 6 and i = 3;<BR><BR>Versus :<BR>__________________________<BR><BR>var i=0;<BR>i++; // i now is 1<BR>__________________________<BR><BR>var i=0;<BR>++1; // i now is also 1<BR>__________________________<BR><BR>I believe this is how it works.<BR><BR>++i increments before calculation<BR>i++ increments after calculation<BR><BR>HtH,<BR><BR>Reginald

3. *THE* Old Fart Guest

## Your boss is an old fart...

4. Senior Member
Join Date
Dec 1969
Posts
11,334

## RE: Your boss is an old fart...

I would say that the example Reggie gave shows the main difference in the two... definately in a for loop, I would think it makes 0 difference.<BR><BR>Although with all that, even using it the way Reggie described would make it hard to read -- to a point. Take the extra line and increment it, I say.<BR><BR>I&#039;ve seen loops written both ways, and even when I&#039;ve changed &#039;em around, makes 0 difference in output.<BR><BR>Me thinks his boss like to micro-manage a bit...

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Reggie showed WHAT...

the difference is, true. But that was *NOT* what the original poster asked/said. He said his boss wanted him to use ++i instead of i++ because it is more *efficient*. <BR><BR>I am sure that even his boss realizes that there are coding circumstances where you *want* to use i++. The classic one is copying a string with a null statement after a while:<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; while ( *src++ = *dest++ );<BR><BR>You *can&#039;t* use *++src or *++dest in that situation or you won&#039;t pick up the first character.<BR><BR>Reggie&#039;s answer was fine, except that it didn&#039;t address the question, I thought.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
11,334

## Question not clear?

I didn&#039;t wonder about the efficieny part... I thought he asked this:<BR><BR>&#062;&#062;but how can you tell which situation to use what? <BR><BR>Meaning how is HE supposed to know WHEN to use it (could be interpreted as efficiency, I guess). Ah well... we&#039;ll probably never hear from him again. <BR><BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## Interesting how we focus...

...on different parts of the same sentence. He actually wrote:<BR><BR>** QUOTE **<BR>I know the difference between the two, but how can you tell which situation to use what? <BR>** UNQUOTE **<BR><BR>I read "I know the difference..." and assumed he understood completely the implications involved. Then the rest of the sentence related, to me, to "which *performance* situation" instead of the effects of the "difference" in code, per se.<BR><BR>Ah, well...It *will* be interesting to see if he responds.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•