2

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!!

kotAPI
  • 1,073
  • 2
  • 13
  • 37
user3858
  • 129
  • 2
  • 3
  • 12
  • 3
    "i++ is executing first, then increment, ++i is increment first, then execute,i+=1 is increment by 1,then execute" - dangerously wrong. `i++` is "increment and evaluate to the original value"; there's no specification of what happens "first". – user2357112 Jan 30 '14 at 03:40
  • @user2357112 Indeed, except when these operators are overloaded, since `operator++` must return before the expression can be considered evaluated. That's why pre-increment/decrement is often preferred - there's no need to create a copy of the original object. – bcrist Jan 30 '14 at 03:52

4 Answers4

6

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.

Paul Draper
  • 78,542
  • 46
  • 206
  • 285
  • 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. – Koushik Shetty Jan 30 '14 at 03:56
4

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

Community
  • 1
  • 1
deeiip
  • 3,319
  • 2
  • 22
  • 33
  • 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 Jan 30 '14 at 03:46
  • 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 Jan 30 '14 at 03:49
  • 1
    No problem. Just remember not to give anything for granted when answering. ;) – Shoe Jan 30 '14 at 03:55
2

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.

Brian Gradin
  • 2,165
  • 1
  • 21
  • 42
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.

Koushik Shetty
  • 2,146
  • 4
  • 20
  • 31
  • And you forgot to mention `i++` returns temp , as in `int temp = i; i = i + 1; return temp;` – john Jan 06 '20 at 05:23