-1

Im learning C and saw the first loop listed below in the book im reading. Im curious whats the difference between the two as I am used to using the second one and cant figure out the difference even though they return different results.

for(i = 0; i < 10; ++i){}

for(i = 0; i <= 10; i++){}
Andrei0427
  • 573
  • 3
  • 6
  • 18
  • 1
    possible duplicate of [Post-increment and pre-increment in 'for' loop](http://stackoverflow.com/questions/4706199/post-increment-and-pre-increment-in-for-loop) – Joe Sep 11 '12 at 09:27
  • There is no change in how the loop runs.The change is how `i` gets incremented.google about `pre and post increment` – tez Sep 11 '12 at 09:32

5 Answers5

6

The first one iterates to 9, the second iterates to 10. That's all.

The pre-/post- increment operation makes no difference.

Un-optimized code generated for both versions:

    for(int i = 0; i < 10; ++i)
00E517AE  mov         dword ptr [i],0  
00E517B5  jmp         wmain+30h (0E517C0h)  
00E517B7  mov         eax,dword ptr [i]  
00E517BA  add         eax,1  
00E517BD  mov         dword ptr [i],eax  
00E517C0  cmp         dword ptr [i],0Ah  
00E517C4  jge         wmain+53h (0E517E3h)  
    {
    }

    for(int i = 0; i <= 10; i++)
00E517E3  mov         dword ptr [i],0  
00E517EA  jmp         wmain+65h (0E517F5h)  
00E517EC  mov         eax,dword ptr [i]  
00E517EF  add         eax,1  
00E517F2  mov         dword ptr [i],eax  
00E517F5  cmp         dword ptr [i],0Ah  
00E517F9  jg          wmain+88h (0E51818h)  
    {
    }

So, even here, there is no performance penalty. The fact that i++ is slower than ++i is just not true (at least in this context, where it doesn't make a difference). It would be slower for, say int y = i++, but in this case, the two would do different things, which is not the case here. The performance issue might have been valid on compilers from 20 years ago, but not anymore.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • "It would be slower for, say `int y = i++;`" - I don't think you can be sure that's slower than `int y = ++i;`. I guess it might depend what opcodes are available to do the increment, but basically those two statements both increment `i`, and copy its value to `y` either before or after the increment. Is there any general reason to think that copying before the increment is more expensive than copying after? – Steve Jessop Sep 11 '12 at 10:17
  • @SteveJessop you're right, not even then, but I'm sure there are cases where indeed the post-increment is slower, but not relevant to this case. – Luchian Grigore Sep 11 '12 at 10:20
  • Btw, when either will work I prefer `++i` for non-performance-related reasons. So I take a keen interest in claims about performance, just in case some performance issue should ever undermine my style preference ;-) – Steve Jessop Sep 11 '12 at 10:20
  • Agreed, I think there could be cases where it's slower. For a hypothetical, imagine that the CPU had a "load and add 1 to the result" instruction as well as loads, stores, and arithmetic on registers. Then `y = ++i` could take advantage of that to "load i+1; store result to i; store result to y;", whereas `y = i++` would either take one more instruction or require a memory-to-memory load-and-store. All assuming that the variables can't be registerized in the particular code, ofc. – Steve Jessop Sep 11 '12 at 10:26
1

In the first you have pre increment, and the second has a post increment.

The only thing is the condition, i.e in the first you are checking upto 9 and in the second its upto 10.

In both the loops the increment operator makes no difference in this case

Shiridish
  • 4,942
  • 5
  • 33
  • 64
  • I have seen programmers prefer ++i for the same reason – Shiridish Sep 11 '12 at 09:36
  • Just a reference: Answer in [this](http://stackoverflow.com/questions/4706199/post-increment-and-pre-increment-in-for-loop) and a comment that the answerer posted (got 6 upvotes too). So I think there are some people who think as I do. – Shiridish Sep 11 '12 at 09:37
  • Well I've never went into the depths of it though. Thanks for the info. – Shiridish Sep 11 '12 at 09:48
  • @Cdeez That comment is incorrect. Needing to remember old value is of `i++` is not true in the case of `for` loop. – P.P Sep 11 '12 at 09:50
  • There is the problem! People like me who gain knowledge from internet look for such stuff, and when see such posts with upvotes tend to follow them. – Shiridish Sep 11 '12 at 09:56
1

The pre-/post- increment operation will work when you will use them while assging values.

Say

i=10;

j = i++;

Here value of i will be 11 but, value of j will be 10. because i will increment after values is assigned to j ie Post Increment

i=10;

j = ++i;

Here value of i will be 11 and value of j will also be 11. because i will increment before value is assigned to j ie Pre Increment

Sachin Mhetre
  • 4,465
  • 10
  • 43
  • 68
0

first one will run 10 times. the second one will run 11 times.

spitfire88
  • 1,596
  • 3
  • 19
  • 31
0

Most people have already stated that the number of iterations differs by one, and that the pre- and post increment do not make any difference here.

For c, I would say the first loop is what you more commonly come across. I think the reason for that is that c uses zero-based arrays, and thus the maximum value of the array (or string, as that's an array of chars) is not used as an index in the array (that would be out of bounds). Thus, when looping through an array of length 10 in this example, the first loop would be the more logical one, since you can safely use i as an index for the array. The second loop would result in an error (probably a segmentation fault).

You say you're used to the second one. I don't know why you're used to that, but I assume some other programming language or the fact that in math, loops (summations and such) run up to the limit inclusive (but often then start at one). Zero-based indices can be slightly frustrating in such cases.

In short, in my experience, you'll find the first loop more often, but there are plenty of use-cases for the second.

As for the ++i versus i++: I'm inclined to the latter, since this part of the for statement happens at the end of the loop. A postfix notation feels therefore more logical. But once again, that doesn't really matter.