< previous page page_511 next page >

Page 511
a value, and
a side effect: the value is stored into the object denoted by the left-hand side.
For example, the expression
delta = 2 * 12
has the value 24 and the side effect of storing this value into delta.
In C++, any expression becomes an expression statement when it is terminated by a semicolon. All three of the following are valid C++ statements, although the first two have no effect whatsoever:
23;
2 * (alpha + beta);
delta = 2 * 12;
The third expression statement is useful because of its side effect of storing 24 into delta.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Assignment Expression A C++ expression with (1) a value and (2) the side effect of storing the expression value into a memory location.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Expression Statement A statement formed by appending a semicolon to an expression.
Because an assignment is an expression, not a statement, you can use it anywhere an expression is allowed. Here is a statement that stores the value 20 into firstInt, the value 30 into secondInt, and the value 35 into thirdInt:
thirdInt = (secondInt = (firstInt = 20) + 10) + 5;
Some C++ programmers use this style of coding, but others find it hard to read and error-prone.
In Chapter 5, we cautioned against the mistake of using the = operator in place of the == operator:
if (alpha = 12)  // Wrong
    .
    .
    .

 
< previous page page_511 next page >