# 3.7 Variable Increment and Decrement Operators: '++', '--'

Variable increment (++) and decrement (--) operators come in two flavors: prefix and postfix. These unary operators have the side effect of changing the value of the arithmetic operand, which must evaluate to a variable. Depending on the operator used, the variable is either incremented or decremented by 1.

These operators are very useful for updating variables in loops where only the side effect of the operator is of interest.

#### Increment Operator ++

Prefix increment operator has the following semantics:

++i adds 1 to i first, then uses the new value of i as the value of the expression. It is equivalent to the following statements.

```i += 1;
result = i;
return result;
```

Postfix increment operator has the following semantics:

j++ uses the current value of j as the value of the expression first, then adds 1 to j. It is equivalent to the following statements:

```result = j;
j += 1;
return result;
```

#### Decrement Operator --

Prefix decrement operator has the following semantics:

--i subtracts 1 from i first, then uses the new value of i as the value of the expression.

Postfix decrement operator has the following semantics:

j-- uses the current value of j as the value of the expression first, then subtracts 1 from j.

##### Examples of Increment and Decrement Operators
```// (1) Prefix order: increment operand before use.
int i = 10;
int k = ++i + --i;  // ((++i) + (--i)). k gets the value 21 and i becomes 10.
--i;                // Only side effect utilized. i is 9. (expression statement)

// (2) Postfix order: increment operand after use.
long i = 10;
long k = i++ + i--;  // ((i++) + (i--)). k gets the value 21L and i becomes 10L.
i++;                 // Only side effect utilized. i is 11L. (expression statement)
```

An increment or decrement operator, together with its operand can be used as an expression statement (see Section 4.3, p. 113).

Execution of the assignment in the second declaration in (1) proceeds as follows :

```k = ((++i) + (--i))          Operands are determined.
k = ( 11   + (--i))          i now has the value 11.
k = ( 11   +  10)            i now has the value 10.
k = 21
```

Expressions where variables are modified multiple times during the evaluation should be avoided, because the order of evaluation is not always immediately apparent.

We cannot associate increment and decrement operators. Given that a is a variable, we cannot write (++(++a)). The reason is that any operand to ++ must evaluate to a variable, but the evaluation of (++a) results in a value.

In the case where the operand is of type char, byte, or short, both binary numeric promotion and an implicit narrowing conversion are performed to achieve the side effect of modifying the value of the operand. In the example below, the int value of (++b) (i.e., 11), is assigned to int variable i. The side effect of incrementing the value of byte variable b requires binary numeric promotion to perform int addition, followed by an implicit narrowing conversion of the int value to byte.

```byte b = 10;
int  i = ++b;        // i is 11, and so is b.
```

The increment and decrement operators can also be applied to floating-point operands. In the example below, the side effect of the ++ operator is overwritten by the assignment.

```double x = 4.5;
x = x + ++x;         // x gets the value 10.0.
```

 List of Figures
 List of Tables
 List of Examples
 Foreword
 Preface
 Chapter 1. Basics of Java Programming
 Chapter 2. Language Fundamentals
 Chapter 4. Declarations and Access Control
 Chapter 5. Control Flow, Exception Handling, and Assertions
 Chapter 6. Object-oriented Programming
 Chapter 7. Nested Classes And Interfaces
 Chapter 10. Fundamental Classes
 Chapter 11. Collections and Maps
 Appendix A. Taking the SCPJ2 1.4 Exam
 Appendix B. Objectives for the SCPJ2 1.4 Exam
 Appendix C. Objectives for the Java 2 Platform Upgrade Exam
 Appendix D. Annotated Answers to Review Questions
 Appendix E. Solutions to Programming Exercises
 Appendix F. Mock Exam
 Appendix G. Number Systems and Number Representation