< previous page page_67 next page >

Page 67
num = num + alpha;
is valid for any value of alpha.
In the examples of expressions so far, we have been careful not to mix integer and floating point values in the same expression. When mixed-type expressions occur, the compiler applies certain rules for converting operands from one type to another. In the next chapter, we discuss those rules.
Increment and Decrement
In addition to the arithmetic operators, C++ provides increment and decrement operators:
++
Increment
--
Decrement

These are unary operators that take a single variable name as an operand. For integer and floating point operands, the effect is to add 1 to (or subtract 1 from) the operand. If num currently contains the value 8, the statement
num++;
causes num to contain 9. You can achieve the same effect by writing the assignment statement
num = num + 1;
but C++ programmers typically prefer the increment operator.
The ++ and -- operators can be either prefix operators
++num;
or postfix operators
num++;
Both of these statements behave in exactly the same way; they add 1 to whatever is in num. The choice between the two is a matter of personal preference.

 
< previous page page_67 next page >