Thursday 14 September 2017

Prefix/Postfix Increment/Decrement Operator

Cited from the Book Java How to Program

An increment or decrement operatorthat’s prefixed to (placed before) a variable is referred to as the prefix increment 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 or postfix decrement operator, respectively.

++a
    Increment a by 1, then use the new value of a in the expression in which a resides.
a++
    Use the current value of a in the expression in which a resides, then increment a by 1.
--b
    Decrement b by 1, then use the new value of b in the expression in which b resides.
b--
    Use the current value of b in the expression in which b resides, then decrement b by 1.

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 a variable.

No comments:

Post a Comment