|
|
|
|
|
|
|
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. |
|
|
|
|
|
|
|
|
In addition to the arithmetic operators, C++ provides increment and decrement operators: |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
causes num to contain 9. You can achieve the same effect by writing the assignment statement |
|
|
|
|
|
|
|
|
but C++ programmers typically prefer the increment operator. |
|
|
|
|
|
|
|
|
The ++ and -- operators can be either prefix operators |
|
|
|
|
|
|
|
|
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. |
|
|
|
|
|