Unary operators

A unary operator works on a single operand (or variable). The three unary operators are increment (++), decrement (--) and the negation operator (-). The increment operator will increase the variable by one, and the decrement operator will decrease the value by one. Used in an assignment statement, however, the increment and decrement operators will allow you to specify whether the variable is to be changed before or after the assignment operation.

The two modes for the increment and decrement operators are prefix and postfix modes, which have the syntax of:

Syntax:
  
  variable++ (postfix) 

and

Syntax:
  
  ++variable (prefix) 

In postfix mode, the variable is incremented after the assignment is made. The code

y = x++;
would assign the original value of x to the variable y, and then add one to the value of x. On the other hand, the code
y = ++x;

would increment the value of x first, and then assign that value to y. In the first example, x is going to have a value one greater than y. In the second code, however, both variables will have the same value.

Finally, the negation operator (-) simply causes a variable to change signs. If 'myVariable' has the value of -5, '-myVariable' would be equal to five.