Unary Operators
The unary operators require only one operand.
Operator | Description |
---|---|
++ | Add by 1(Incrementing) |
-- | Subtract 1(Decrementing) |
Example Incrementing
int total = 10;
++total; //total is now 11
int total = 10;
--total; //total is now 9
The increment/decrement operators can be applied before (prefix) or after (postfix) the operand.
Prefix
In prefix, the increment happens first and then the expression will be evaluated. This results in the mark
value is incremented to 11 and then assigned to adjustedMark
.
int mark = 10;
int adjustedMark = ++mark; //adjustedMark will be 11
Postfix
In postfix, the expression is evaluated first and then the increment gets evaluated afterwards.
int mark = 10;
int adjustedMark = mark++; //adjustedMark will be 10