2. When the app executes, another compiler (known as the just-in-time compiler
5.12 Increment and Decrement Operators
C# provides twounaryoperators for adding 1 to or subtracting 1 from the value of a nu- meric variable. These are the unaryincrement operator,++, and the unarydecrement op- erator,--, respectively, which are summarized in Fig. 5.14. An app can increment by1the
Error-Prevention Tip 5.4
Initializing local variables when they’re declared helps you avoid compilation errors that might arise from attempts to use uninitialized data. While C# does not require that local- variable initializations be incorporated into declarations, it does require that local vari- ables be initialized before their values are used in an expression.
variable = variable operator expression;
variable operator= expression;
c = c + 3;
c += 3;
Assignment operator
Sample
expression Explanation Assigns
Assume: int c = 3, d = 5, e = 4, f = 6, g = 12;
+= c += 7 c = c + 7 10toc
-= d -= 4 d = d - 4 1tod
*= e *= 5 e = e * 5 20toe
/= f /= 3 f = f / 3 2tof
%= g %= 9 g = g % 9 3tog
Fig. 5.13 | Arithmetic compound assignment operators.
value of a variable calledcusing the increment operator, ++, rather than the expression
c = c + 1orc += 1. An increment or decrement operator that’s prefixed to (placedbefore) a variable is referred to as theprefix increment operatororprefix decrement operator, re- spectively. An increment or decrement operator that’s postfixed to (placedafter) a variable is referred to as thepostfix increment operatororpostfix decrement operator, respectively.
Incrementing (or decrementing) a variable with anincrement(or decrement) operator causes it to be incremented (or decremented) by 1. For aprefix increment(or decrement) operator, the variable’snewvalue is used in the expression in which the variable appears.
For apostfix increment(or decrement) operator, the variable’soriginalvalue is used in the expression in which the variable appears.
Figure 5.15 demonstrates the difference between the prefix increment and postfix increment versions of the++increment operator. The decrement operator (--) works sim- ilarly. In this example, we simply want to show the mechanics of the++operator, so we use only one class declaration containing methodMain.
Operator Called
Sample
expression Explanation
++ prefix
increment
++a Incrementsaby1and uses the new value ofa in the expression in whicharesides.
++ postfix
increment
a++ Incrementsaby1, but uses the original value ofain the expression in whicharesides.
-- prefix
decrement
--b Decrementsbby1and uses the new value of
bin the expression in whichbresides.
-- postfix
decrement
b-- Decrementsbby1but uses the original value ofbin the expression in whichbresides.
Fig. 5.14 | Increment and decrement operators.
Good Programming Practice 5.7
Unlike binary operators, the unary increment and decrement operators should (by con- vention) be placed next to their operands, with no intervening spaces.
1 // Fig. 5.15: Increment.cs
2 // Prefix increment and postfix increment operators.
3 using System;
4
5 public class Increment 6 {
7 public static void Main( string[] args )
8 {
9 int c;
10
Fig. 5.15 | Prefix increment and postfix increment operators. (Part 1 of 2.)
5.12 Increment and Decrement Operators 173
Line 12 initializes the variablecto5, and line 13 outputsc’s initial value. Line 14 out- puts the value of the expressionc++. This expression performs the postfix increment oper- ation on the variablec, so even thoughc’s value is incremented,c’soriginalvalue (5) is output. Thus, line 14 outputsc’s initial value (5) again. Line 15 outputsc’s new value (6) to prove that the variable’s value was indeed incremented in line 14.
Line 20 resetsc’s value to5, and line 21 outputsc’s value. Line 22 outputs the value of the expression++c. This expression performs the prefix increment operation onc, so its value is incremented and thenewvalue (6) is output. Line 23 outputsc’s value again to show that the value ofcis still6after line 22 executes.
The arithmetic compound assignment operators and the increment and decrement operators can be used to simplify statements. For example, the three assignment state- ments in Fig. 5.12 (lines 24, 26 and 29)
can be written more concisely with compound assignment operators as
and even more concisely with prefix increment operators as 11 // demonstrate postfix increment operator 12 c = 5; // assign 5 to c
13 Console.WriteLine( c ); // display 5 14
15 16
17 Console.WriteLine(); // skip a line 18
19 // demonstrate prefix increment operator 20 c = 5; // assign 5 to c
21 Console.WriteLine( c ); // display 5 22
23
24 } // end Main
25 } // end class Increment 5
5 6 5 6 6
passes = passes + 1;
failures = failures + 1;
studentCounter = studentCounter + 1;
passes += 1;
failures += 1;
studentCounter += 1;
++passes;
++failures;
++studentCounter;
Fig. 5.15 | Prefix increment and postfix increment operators. (Part 2 of 2.)
Console.WriteLine( c++ ); // increment c and display 5 Console.WriteLine( c ); // display 6
Console.WriteLine( ++c ); // increment c and display 6 Console.WriteLine( c ); // display 6 again
or with postfix increment operators as
When incrementing or decrementing a variable in a statement by itself, the prefix increment and postfix increment forms have thesameresult, and the prefix decrement and postfix decrement forms have thesameresult. It’s only when a variable appears in the con- text of a larger expression that the prefix increment and postfix increment havedifferent results (and similarly for the prefix decrement and postfix decrement).
Precedence and Associativity of the Operators We’ve Discussed So Far
Figure 5.16 shows the precedence and associativity of the operators we’ve 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 decre- ment (--), plus (+) and minus (-); the cast operators; and the assignment operators=,+=,
-=,*=,/=and%=associate from right to left. All the other operators in the operator pre- cedence chart in Fig. 5.16 associate from left to right. The third column names the groups of operators.