-6

To increment an int "i", I can use any of the following:

i++;
i = i + 1;
i = i++;

...with the same result, as this code shows:

int finalVal = 0;
for (int i = 0; i < 42; i++)
{
    //i++; 
    //i = i + 1; 
    i = i++; 
    finalVal = i;
}
MessageBox.Show(finalVal.ToString()); // in each case I get "41"

Is there any reason to prefer one style of incrementing an int over the other, or is it a case of "six of one, a half dozen of the other, and 6.0 of the yet other"?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

4 Answers4

3

They are not same.

  • i++; would give the value of i before increment, incremented value would be visible on next usage of i

Sample Code:

int i = 1;
Console.WriteLine(i++); //print 1
Console.WriteLine(i);   //print 2
  • i = i + 1; would increment i and you will see the change right away.
  • i = i++; , doesn't change the value of i, since i++ would return the original value not the incremented one and that will be assigned back to i, keeping the original value.

The reason your code prints 41 is because you are not changing the value of i inside your loop, with i = i++, i is incremented due to the loop's i++ and that value is retained by finalVal

int finalVal = 0;
for (int i = 0; i < 42; i++) //i is incremented here
{
    //i++; 
    //i = i + 1; 
    i = i++;      //No Change in i , this statement equals to i = i;
    finalVal = i; // finalVal is keeping value of i
}
Habib
  • 219,104
  • 29
  • 407
  • 436
1

I would prefer i++ over the other two because it expresses a common idiom.

Those lines of code all end up doing exactly the same thing. But if I'm scanning code for an increment, the ++ just sort of jumps off the page.

Also, the i = i++ is actually incorrect. You meant i = ++i.

Short answer: Technically no, but stylistically, yes.

Chris Cudmore
  • 29,793
  • 12
  • 57
  • 94
1

I've seen i++ cause confusion for people new to programming, because it's anything but obvious by the syntax alone that it evaluates to the previous value of i. Even you got it wrong:

i = i++;

This does not do the same thing as

i = ++i;

or

i = i + 1;

Furthermore, in C++, i++ can be less efficient as it may require the construction of an unnecessary temporary object.

Asik
  • 21,506
  • 6
  • 72
  • 131
  • I bet you it is at least somewhat inefficient in C# too. The reference for the postfix ops is here: http://msdn.microsoft.com/en-us/library/aa691363%28v=vs.71%29.aspx, and an elaborate discussion by Eric Lippert is here: http://stackoverflow.com/questions/3346450/what-is-the-difference-between-i-and-i/3346729#3346729. The point is the saving of the operand in a temporary. That probably is just a memcpy, but still. (And, for user defined types, couldn't that be semantically wrong?) If not, say if Clone() was invoked, it could be really expensive, but that should be mentioned somewhere. – Peter - Reinstate Monica Jun 25 '14 at 19:24
1

I would rewrite your code something like this to fully understand what's going on.

int ipp, ipone, ippass;
ipp = ipone = ippass =  0;
for (int i = 0; i < 5; i++)
{
    ipp++; 
    ipone = i + 1; 
    ippass = ippass++; 

    MessageBox.Show(" Loop Counter: " + i + "\n IPlusPlus: " + ipp + "\n IPlusOne: " + ipone + "\n IPlusPlusAssigned: " + ippass );

}

You might want to lower the loop count, as you're going to get a whole lot of message boxes.

Chris Cudmore
  • 29,793
  • 12
  • 57
  • 94