JGPARK
JGPARK

Reputation: 57

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?

Upvotes: 1

Views: 445

Answers (6)

student402
student402

Reputation: 1

The pre increment and post increment work same for a "for" loop as it only a condition to add one to the integer i.For example given as for loop will print same as 01234

#include <stdio.h>

int main() {
    for (int i = 0; i < 5; ++i) {
        printf("%d", i);
    }
    for (int i = 0; i < 5; i++) {
        printf("%d", i);
    }
    return 0;
}

01234 01234

The different lies when you use it for a print statement like in a while loop or initialize i as 0 and print i++ and ++i as in "post" i++ increment is after executing the snippet while in "pre" ++i it first add i=i+1 and then output is given.

#include <stdio.h>

int main() {
    int i = 0;
    printf("%d", ++i); // Pre-increment: i is incremented before it is printed
    i = 0;
    printf("%d", i++); // Post-increment: i is printed before it is incremented
    return 0;
}


1
0

Upvotes: -1

chqrlie
chqrlie

Reputation: 144949

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;
}

Upvotes: 1

compsi
compsi

Reputation: 21

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.

Upvotes: 0

mmixLinus
mmixLinus

Reputation: 1714

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.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409364

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).

Upvotes: 3

Lundin
Lundin

Reputation: 214395

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.

Upvotes: 4

Related Questions