简体   繁体   中英

Why ++i, i++ are the same in for loop?

for (int i = 0; i < n+1; ++i)
{
    sum = sum + i;
}

for (int i = 0; i < n+1; i++)
{
    sum = sum + i;
}

Two paragraphs are different because of ++i and i++ in function call argument.

but it works like i only starts with 0. Why does even ++i starts with 0?

There are absolutely no difference between these two snippets. i++ vs ++i only matters when mixed with other operators in the same expression. Which is a bad idea most of the time, since i++ / ++i comes with a side effect.

A generic for loop like

for (a; b; c)
{
    d
}

is equivalent to

{
    a;
    while (b)
    {
        d;
        c;
    }
}

Note how the "increment" expression c is after the main statement of the loop body.

For your loops that means they will be equivalent to:

{
    int i = 0;
    while (i < n+1)
    {
        sum = sum + i;
        i++;  // or ++i
    }
}

Since the increment of i doesn't happen until after you calculate sum there's no practical difference between the loops. Both will lead to the exact same result.


On a side-note: Remember to explicitly initialize sum to zero before the loop, or it might have an indeterminate value (that could be seen as garbage).

The two for loops you posted will behave exactly the same. The reason they are equivalent, which also answers your question of why both loops start from 0 , is that the increment ( i++ or ++i ) only happens after the {expression} in {} has executed, and after every time it has executed.

Your code would produce identical results.

The following code would produce different results.

int i = 0;
while (i < 5) {
  printf("%d\n", ++i);
}
// 1
// 2
// 3
// 4
// 5
i = 0;
while (i < 5) {
  printf("%d\n", i++);
}
// 0
// 1
// 2
// 3
// 4

In my example the reason the bottom for-loop prints out 0 and the top for-loop doesn't is because printf and i++/++i form a combined expression where in the top i is incremented and then accessed from memory whereas in the bottom loop i is access from memory and then incremented.

The i++ and ++i expressions in the last clause of these for loops both increment i as a side effect and since the value of the expression is not used, they have exactly the same effect, namely the side effect.

Here are other alternative, all of which behave the same and should produce the same code:

for (int i = 0; i < n+1; i += 1) {
    sum = sum + i;
}

and

for (int i = 0; i < n+1; i = i + 1) {
    sum = sum + i;
}

Note however that if n is an int with the value INT_MAX , n+1 has undefined behavior. A safer way to write this loop is:

for (int i = 1; i <= n; i++) {
    sum = sum + i;
}

But the <= operator has the same problem when n is INT_MAX : the undefined behavior would occur on the i++ part when i reaches INT_MAX ...

Here is an alternative that avoids this corner case:

if (n > 0) {
    for (int i = 1; i < n; i++) {
        sum = sum + i;
    }
    sum = sum + n;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM