Increment ++i, i++ and i+=1

18,456

Solution 1

I think if you imagine how the for loop works you can understand the problem at hand.

for loop

initialization    --> i = 0

<check condition>  --> i<10? ------->--------------------
 |                      |                                |
 ^                      |yes(i.e condition not met)      |no(i.e condition met)
 |                      V                                V
 |                  --------------
 |                  |body of loop|
 |                  --------------
 |                      |
 |                      V
 -------<------------increment i (**)                exit for loop

** increment i means i++ or ++i

i++ can be replaced by this :

int temp = i;
i = i + 1;

temp will be useless here so the compiler will optimize it to just inc i instruction. even if it doesn't do that temp is just a waste of space that's all.

++i can be replaced by

i = i + 1;
int temp = i; 

again temp is not required so the compiler will just replace it with inc i instruction.

if you see both the instruction are the same because they are not being assigned to anything. only i is being affected by the increment. so both are essentially the same. this is true if i is a built-in type .

you see that the increment instruction is placed after the body of the loop? can you now see that this is almost similar to the while loop that you showed?

it is always nice to think of loops in this way.

Solution 2

You are correct. In your examples there is no difference.

But here there is:

int i = 0;
cout << i++ << endl;  //prints 0
cout << i   << endl;  //prints 1

vs

int i = 0;
cout << ++i << endl;  //prints 1
cout <<   i << endl;  //prints 1

Which one of those is used most in programming?

Most of the time, ++ is the only operation in the statement (FYI, a for loop has three statements).

If it isn't, then it might matter, and you'll use whichever one gives you the correct behavior.

FYI

Some developers have the opinion that if pre and postfix operators should always be used alone (not part of a large statement). They can lead to confusing code, or even undefined behavior.

For example,

int i = 0;
cout << i++ + i++ << endl;

has undefined behavior.

Solution 3

So could anyone explain this to me?

What i++ does is return the current value of i and then increment it by one, and ++i first increment i by 1 and then returns the value of i.

Take a look at this, for example:

i = 5;
j = 5;
res = i++;    //res = 5, but i=6
res = ++j;    //res = 6 and j=6

Which one of those is used most in programming?

Both are used depending on what you want or may be how you want.


One more thing to note: ++i is an l-value, but i++ is not. For details see here

Solution 4

Your analysis is correct. i++ will return the value of i, then increment, whereas ++i will increment the value of i, then return the new value. i += 1 will do the same as ++i. The difference in where they will be used in actual code is primarily situational; there's no specific answer as to where each of them are most often used or helpful. It all depends on what you want to do.

Here's a contrived example of one time it might be useful to use post-increment (i++ form):

// Return the length of a string, including the null byte
int stringLength(char str[])
{
    int length = 0;
    while (str[length++] != 0);
    return length;
}

If you wanted to return the length without the null byte, you could modify the above example slightly, using the ++i form:

// Return the length of a string, without the null byte
int stringLength(char str[])
{
    int length = -1;
    while (str[++length] != 0);
    return length;
}

As for i += 1, I don't think I've ever done quite that, since you can use pre- or post-increment instead. I've generally only used the compound assignment operators for values other than 1.

Share:
18,456
user3858
Author by

user3858

Updated on June 04, 2022

Comments

  • user3858
    user3858 almost 2 years

    I am beginner in C++. What I understand is that:-

    i++ is executing first, then increment, ++i is increment first, then execute,i+=1 is increment by 1,then execute. But in the FOR loop:

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

    There is really no difference in these two loops above.

    Here is another one to calculate the sum of all integers from 1 to 100:

    int i=1, sum=0;
    while (i<=100)
    {
        sum+=i;
        i++;         //i+=1;    ++i;
    }
    cout<<sum<<" "<<i<<endl;
    return 0;
    

    But if I replace i++ with i+=1 or ++i, they all return a sum of 5050 and i of 101. So I really don't see any difference in them.

    So could anyone explain this to me? Which one of those is used most in programming? Thank you!!

  • Shoe
    Shoe about 10 years
    To the first question you answer with "This is the explanation of difference between i++ and ++i", but you don't provide any explanation. Are you ok, sir?
  • Shoe
    Shoe about 10 years
    You are just showing some code without explaining it or answering any question at all. While the code explanation might be obvious to you, it is not to those who read it.
  • Shoe
    Shoe about 10 years
    No problem. Just remember not to give anything for granted when answering. ;)
  • Koushik Shetty
    Koushik Shetty about 10 years
    I think it will help the OP if you split the for loop and show him exactly how it can be imagined. that will give a solid foundation.
  • john
    john over 4 years
    And you forgot to mention i++ returns temp , as in int temp = i; i = i + 1; return temp;