338 Chapter 7 Expressions and Assignment Statements
7.7.4 Unary Assignment Operators
The C-based languages, Perl, and JavaScript include two special unary arith-
metic operators that are actually abbreviated assignments. They combine
increment and decrement operations with assignment. The operators ++
for increment, and –– for decrement, can be used either in expressions or to
form stand-alone single-operator assignment statements. They can appear
either as prefix operators, meaning that they precede the operands, or as
postfix operators, meaning that they follow the operands. In the assignment
statementsum = ++ count;the value of count is incremented by 1 and then assigned to sum. This opera-
tion could also be stated ascount = count + 1;
sum = count;If the same operator is used as a postfix operator, as insum = count ++;the assignment of the value of count to sum occurs first; then count is incre-
mented. The effect is the same as that of the two statementssum = count;
count = count + 1;An example of the use of the unary increment operator to form a complete
assignment statement iscount ++;which simply increments count. It does not look like an assignment, but it
certainly is one. It is equivalent to the statementcount = count + 1;When two unary operators apply to the same operand, the association is
right to left. For example, in- count ++
count is first incremented and then negated. So, it is equivalent to- (count ++)