< previous page page_512 next page >

Page 512
else
    .
    .
    .
The condition in the If statement is an assignment expression, not a relational expression. The value of the expression is 12 (interpreted by the computer as TRUE), so the else-clause is never executed. Worse yet, the side effect of the assignment expression is to store 12 into alpha, destroying its previous contents.
In addition to the = operator, C++ has several combined assignment operators (+=, *=, and the others listed in our table of operators). These operators have the following semantics:
Statement
Equivalent Statement
i += 5;
i = i + 5;
pivotPoint *= n + 3;
pivotPoint = pivotPoint * (n + 3);

The combined assignment operators are another example of icing on the cake. They are sometimes convenient for writing a line of code more compactly, but you can do just fine without them.
Increment and Decrement Operators
The increment and decrement operators (++ and --) operate only on variables, not on constants or arbitrary expressions. Suppose a variable someInt contains the value 3. The expression ++someInt denotes pre-incrementation. The side effect of incrementing someInt occurs first, so the resulting value of the expression is 4. In contrast, the expression someInt++ denotes post-incrementation. The value of the expression is 3, and then the side effect of incrementing someInt takes place. The following code illustrates the difference between pre-and post-incrementation:
int1 = 14;
int2 = ++int1;
// Assert: int1 == 15  &&  int2 == 15

int1 = 14;
int2 = int1++;
// Assert: int1 == 15  &&  int2 == 14
Using side effects in the middle of larger expressions is always a bit dangerous. It's easy to make semantic errors, and the code may be confusing to read. Look at this example:
a = (b = c++) * --d / (e += f++);

 
< previous page page_512 next page >