5.13 Increment and Decrement Operators

C# provides two unary operators for adding 1 to or subtracting 1 from the value of a numeric variable (summarized in Fig. 5.15). These are the unary increment operator, ++, and the unary decrement operator, --, respectively. An app can increment by 1 the value of a variable called c using the increment operator, ++, rather than the expression c = c + 1 or c += 1. An increment or decrement operator that’s prefixed to (placed before) a variable is referred to as the prefix increment operator or prefix decrement operator, respectively. An increment or decrement operator that’s postfixed to (placed after) a variable is referred to as the postfix increment operator or postfix decrement operator, respectively.

Using the prefix increment (or decrement) operator to add 1 to (or subtract 1 from) a variable is known as preincrementing (or predecrementing). This causes the variable to be incremented (decremented) by 1; then the new value of the variable is used in the expression in which it appears.

Fig. 5.15 Increment and decrement operators.

Operator Sample expression Explanation
++ (prefix increment) ++a Increments a by 1 and uses the new value of a in the expression in which a resides.
++ (postfix increment) a++ Increments a by 1, but uses the original value of a in the expression in which a resides.
-- (prefix decrement) --b Decrements b by 1 and uses the new value of b in the expression in which b resides.
-- (postfix decrement) b-- Decrements b by 1 but uses the original value of b in the expression in which b resides.

Using the postfix increment (or decrement) operator to add 1 to (or subtract 1 from) a variable is known as postincrementing (or postdecrementing). This causes the current value of the variable to be used in the expression in which it appears; then the variable’s value is incremented (decremented) by 1.

Good Programming Practice 5.4

Unlike binary operators, the unary increment and decrement operators as a matter of style should be placed next to their operands, with no intervening spaces.

5.13.1 Prefix Increment vs. Postfix Increment

Figure 5.16 demonstrates the difference between the prefix-increment and postfix-increment versions of the ++ increment operator. The decrement operator (--) works similarly.

Fig. 5.16 Prefix-increment and postfix-increment operators.

Alternate View

  1   // Fig. 5.16: Increment.cs
  2   // Prefix increment and postfix increment operators.
  3   using System;
  4
  5   class Increment
  6   {
  7      static void Main()
  8      {
  9         // demonstrate postfix increment operator
 10         int c = 5; // assign 5 to c
 11         Console.WriteLine($"c before postincrement: {c}"); // displays 5
 12         Console.WriteLine($"    postincrementing c: {c++}"); // displays 5
 13         Console.WriteLine($" c after postincrement: {c}"); // displays 6
 14
 15         Console.WriteLine(); // skip a line
 16
 17         // demonstrate prefix increment operator
 18         c = 5; // assign 5 to c
 19         Console.WriteLine($" c before preincrement: {c}"); // displays 5
 20         Console.WriteLine($"     preincrementing c: {++c}"); // displays 6
 21         Console.WriteLine($"  c after preincrement: {c}"); // displays 6
 22      }
 23   }

c before postincrement: 5
    postincrementing c: 5
 c after postincrement: 6

c before preincrement: 5
    preincrementing c: 6
 c after preincrement: 6

Line 10 initializes the variable c to 5, and line 11 outputs c’s initial value. Line 12 outputs the value of the expression c++. This expression postincrements the variable c, so c’s original value (5) is output, then c’s value is incremented (to 6). Thus, line 12 outputs c’s initial value (5) again. Line 13 outputs c’s new value (6) to prove that the variable’s value was indeed incremented in line 12.

Line 18 resets c’s value to 5, and line 19 outputs c’s value. Line 20 outputs the value of the expression ++c. This expression preincrements c, so its value is incremented; then the new value (6) is output. Line 21 outputs c’s value again to show that the value of c is still 6 after line 20 executes.

5.13.2 Simplifying Increment Statements

The arithmetic compound assignment operators and the increment and decrement operators can be used to simplify statements. For example, the three assignment statements in Fig. 5.13 (lines 24, 28 and 32)


passes = passes + 1;
failures = failures + 1;
studentCounter = studentCounter + 1;

can be written more concisely with compound assignment operators as


passes += 1;
failures += 1;
studentCounter += 1;

and even more concisely with prefix-increment operators as


++passes;
++failures;
++studentCounter;

or with postfix-increment operators as


passes++;
failures++;
studentCounter++;

When incrementing or decrementing a variable in a statement by itself, the prefix increment and postfix increment forms have the same effect, and the prefix decrement and postfix decrement forms have the same effect. It’s only when a variable appears in the context of a larger expression that the prefix increment and postfix increment have different results (and similarly for the prefix decrement and postfix decrement).

Common Programming Error 5.5

Attempting to use the increment or decrement operator on an expression other than one to which a value can be assigned is a syntax error. For example, writing ++(x + 1) is a syntax error, because (x + 1) is not an expression to which a value can be assigned.

5.13.3 Operator Precedence and Associativity

Figure 5.17 shows the precedence and associativity of the operators introduced to this point. The operators are shown from top to bottom in decreasing order of precedence. The second column describes the associativity of the operators at each level of precedence. The conditional operator (?:); the unary operators prefix increment (++), prefix decrement (--), plus (+) and minus (-); the cast operators; and the assignment operators =, +=, -=, *=, /= and %= associate from right to left. All the other operators in the operator precedence chart in Fig. 5.17 associate from left to right. The third column names the groups of operators.

Fig. 5.17 Precedence and associativity of the operators discussed so far.

Operators Associativity Type
. new ++(postfix) --(postfix) left to right highest precedence
++ -- + - (type) right to left unary prefix
* / %       left to right multiplicative
+ -         left to right additive
< <= > >=     left to right relational
== !=         left to right equality
?:           right to left conditional
= += -= *= /= %= right to left assignment
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset