Increment and decrement operators
Increment and decrement operators can be applied only to the
int
variables.The increment operator increases the value of the int
variable
by 1. The decrement operator decreases the value of int
variable by 1.
These operators can be used as a prefix (before the variable) or suffix (after the variable). The prefix version first modifies the variable value, and then it is used. The suffix version first uses the value, and then modifies it.
These are examples of increment and decrement operators:
int i = 10;
int a = i++; // The value of 'i' is first assigned to variable 'a' and then gets incremented.
int b = ++i; // The value of 'i' is first incremented and then it gets assigned into the variable 'a'.
int c = a--; // The value of 'a' is first assigned to variable 'c' and then gets decremented.
int d = --b; // The value of 'b' is first decremented and then it gets assigned into the variable 'd'.