Increment and Decrement Operators

Purpose:

Shortcuts for increasing and decreasing numeric type variables.

Mechanics:

Operator Example Description
++ counter++; or ++counter; Adds one to the variable and stores the new number in that variable.
-- counter--; or --counter; Subtracts one from the variable and stores the new number in that variable.
+= counter += 2; Adds the specified number to the variable and stores the new number in that variable.
-= counter -= 2; Subtracts the specified number from the variable and stores the new number in that variable.
*= counter *= 2; Multiples the variable by the specified number and stores the new number in that variable.
/= counter /= 2; Divides the variable by the specified number and stores the integer result in that variable.

Usage:

  • Variable increment and decrement operators ++ and -- can either be used as a prefix (++counter) or a postfix (counter--). When used as a prefix, the variable is incremented (or decremented) and then used:
    int i = 10;
    int j = ++i; // i is incremented to 11, and then j is set to 11 (the value of i).
    When used as a postfix, the variable is used and then incremented:
    int i = 10;
    int j = i++; // j is set to 10 (the value of i) and then i is incremented to 11.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu