# Different Behaviour between ++ and += 1 [closed]

void AddItem(Item item)
{
int indexOfDuplicateItem = System.Array.IndexOf(itemsInInventory, item);

if (indexOfDuplicateItem > -1)
{
quantityText.text = (stackSize[indexOfDuplicateItem] += 1).ToString(); // Problem occurs here
}
else
{
for (int i = 0; i < numSlots; i++)
{
if (itemsInInventory[i] == null)
{
itemsInInventory[i] = item;
GameObject itemImage = Instantiate(itemImagePrefab, slots[i].transform);
itemImage.GetComponent<Image>().sprite = item.itemSprite;
quantityText = slots[i].GetComponentInChildren<Text>();
stackSize[i] = item.itemQuantity;
quantityText.text = stackSize[i].ToString();
return;
}
}
}


The indicated line behaves differently if I write quantityText.text = (stackSize[indexOfDuplicateItem]++).ToString();

I'm a relative beginner to both C# and Unity. Can someone explain why the behaviour of (stackSize[indexOfDuplicateItem]++).ToString() is different from (stackSize[indexOfDuplicateItem] += 1).ToString()?

using += 1 to increment behaves correctly, but ++ ends up being the correct answer - 1.

• For syntax questions, don't neglect to google it / search Stack Overflow first - they have good coverage of common questions about language fundamentals. – DMGregory Oct 12 '17 at 22:42
• Indeed, this is a very basic issue with programming syntax (not that it is a bad question for a programming Q&A, but it has nothing to do with game dev, itself). – Gnemlock Oct 15 '17 at 3:10

This is expected behaviour given the usual syntax for ++, which has been around since the language B (circa 1969):

• variable++ is a "post-increment" operator, ie. "Return to me the current value of the variable, then increment that variable afterward"

So an expression containing this construct gets evaluated with the old value of the variable, before it was incremented.

This is useful in a loop where you want to ensure the next use of a variable is incremented, eg...

int p = 0; for(y = 0; y < height; y++) for(x = 0; x < width; x++) pixel_buffer[p++] = GetPixelColor(x, y); 

This loop will write to pixel_buffer[0] first, then each subsequent index in turn.

• ++variable is a "pre-increment" operator, ie. "Increment the value of the variable before returning it, then return to me its new value"

• variable += 1 is an (augmented) assignment expression. When it's evaluated, the whole expression gets run (incrementing the variable), then the resulting value of the variable is treated as its return value. So it behaves similarly to ++variable

This is most likely because of how the ++ operator works. The ++ operator works in two steps:

1. Returns the value if the variable
2. Increments the variable

This means that when you do stackSize[indexOfDuplicateItem]++ it first returns stackSize[indexOfDuplicateItem] THEN increases the value by 1. It's the same as writing this:

quantityText.text = (stackSize[indexOfDuplicateItem]).ToString();
stackSize[indexOfDuplicateItem] += 1


This is a small test case:

>>> int i = 5;
>>> Console.Write(i)
5
>>> Console.Write(i++)
5 // STILL 5!
>>> Console.Write(i)
6 // Now 6!


To circumvent this you could put the ++ before like so: ++stackSize[indexOfDuplicateItem]. This will increment the value by 1 and then return the value. This should give the desired result.

Consider the following:

int i = 0
strings[] myArray = ["a","b","c","d"];

...

Debug.Log(myArray[i++]); // Evaluates i first, which is zero -> log = "a"
// Then add 1 to i

Debug.Log(myArray[i++]); // Evaluates i first, which is 1 -> log =  "b"
// Then add 1 to i


Now we can also do ++ on the left side

int i = 0
strings[] myArray = ["a","b","c","d"];

...

Debug.Log(myArray[++i]); // add one to i first, then evaluate i -> log =  "b"

Debug.Log(myArray[++i]); // add one to i first, then evaluate i -> log =  "c"