DekGenius.com
Team LiB   Previous Section   Next Section

7.3 Increment and Decrement Operators

A common requirement is to add a value to a variable, subtract a value from a variable, or otherwise change the mathematical value, and then to assign that new value back to the original variable.

7.3.1 Calculate and Reassign Operators

Suppose you want to increment the mySalary variable by 5000. You can do this by writing:

mySalary = mySalary + 5000;

In simple arithmetic, this would make no sense, but in C# this line means "add 5000 to the value in mySalary, and assign the sum back to mySalary." Thus, after this operation completes, mySalary will have been incremented by 5000. You can perform this kind of assignment with any mathematical operator:

mySalary = mySalary * 5000;
mySalary = mySalary - 5000;

and so forth.

The need to perform this kind of manipulation is so common that C# includes special operators for self-assignment. Among these operators are +=, -=, *=, /=, and %=, which, respectively, combine addition, subtraction, multiplication, division, and modulus, with self-assignment. Thus, you can write the previous examples as:

mySalary += 5000;
mySalary *= 5000;
mySalary -= 5000;

These three instructions, respectively, increment mySalary by 5000, multiply mySalary by 5000, and subtract 5000 from the mySalary variable.

7.3.2 Increment or Decrement by 1

Because incrementing and decrementing by exactly 1 is a very common need, C# provides two additional special operators for these purposes: increment (++) and decrement (--).

Thus, if you want to increment the variable myAge by 1 you can write:

myAge++;

7.3.3 The Prefix and Postfix Operators

To complicate matters further, you might want to increment a variable and assign the results to a second variable:

resultingValue = originalValue++;

The question arises: do you want to assign before you increment the value or after? In other words, if originalValue starts out with the value 10, do you want to end with both resultingValue and originalValue equal to 11, or do you want resultingValue to be equal to 10 (the original value) and originalValue to be equal to 11?

C# offer two specialized ways to use the increment and decrement operators: prefix and postfix. The way you use the ++ operator determines the order in which the increment/decrement and assignment take place. The semantics of the prefix increment operator is "increment the original value, and then assign the incremented value to result" while the semantics of the postfix increment operator is "assign the original value to result, and then increment original."

To use the prefix operator to increment, place the ++ symbol before the variable name; to use the postfix operator to increment, place the ++ symbol after the variable name.

result = ++original; // prefix
result = original++; // postfix

It is important to understand the different effects of prefix and postfix, as illustrated in Example 7-4. Note the output.

Example 7-4. Prefix and postfix operators
using System;
class Values
{
    static void Main()
    {
        int original = 10;
        int result;
        
        // increment then assign
        result = ++original;
        Console.WriteLine("After prefix: {0}, {1}", original, 
            result);

        // assign then increment
        result = original++;
        Console.WriteLine("After postfix: {0}, {1}", 
            original, result);
    }
}
After prefix: 11, 11
After postfix: 12, 11

The prefix and postfix operators can be applied, with the same logic, to the decrement operators, as shown in Example 7-5. Again, note the output.

Example 7-5. Decrementing prefix and postfix
using System;
class Values
{
    static void Main()
    {
        int original = 10;
        int result;
        
        // increment then assign
        result = --original;
        Console.WriteLine("After prefix: {0}, {1}", original, 
            result);

        // assign then increment
        result = original--;
        Console.WriteLine("After postfix: {0}, {1}", 
            original, result);
    }
}

Output:
After prefix: 9, 9
After postfix: 8, 9
    Team LiB   Previous Section   Next Section