[ fromfile: arithmetic.xml id: arithmetic ]
Each programming language must provide facilities for doing basic arithmetic. For each of its native numerical types, C++ provides these four basic arithmetic operators:
addition (+)
subtraction (-)
multiplication (*)
division (/)
These operator symbols are used to form expressions in the standard infix syntax that we learned in math class.
C++ provides shortcut operators that combine each of the basic operators with the assignment operator (=) so that, for example, it is possible to write
x += y;
instead of
x = x + y;
C++ also provides unary increment (++) and decrement (--) operators which can be used with integral types. If one of these operators is applied to a variable on the left (prefix), then the operation is performed before the rest of the expression is evaluated. If it is applied to a variable on the right (postfix), then the operation is performed after the rest of the expression is evaluated. Modern compilers will usually produce a shorter block of machine code when compiling the prefix operator than when compiling the postfix operator, so prefix operators are recommended over postfix operators when it doesn't matter whether the operation takes place before or after the expression is evaluated. Example 1.18 through Example 1.22 demonstrate the use of the C++ arithmetic operators.
Example 1.18. src/arithmetic/arithmetic.cpp
[ . . . . ] #include <QTextStream> int main() { QTextStream cout(stdout); double x(1.23), y(4.56), z(7.89) ; int i(2), j(5), k(7); x += y ; z *= x ; cout << "x = " << x << "\tz = " << z << "\nx - z = " << x - z << endl ;
Integer division is handled as a special case.
The result of dividing one int by another produces an int quotient and an int remainder.
The operator / is used to obtain the quotient.
The operator % (called the modulus operator) is used to obtain the remainder.
Example 1.19 demonstrates the use of these integer arithmetic operators.
Example 1.19. src/arithmetic/arithmetic.cpp
[ . . . . ]
cout << "k / i = " << k / i
<< "\tk % j = " << k % j << endl ;
cout << "i = " << i << "\tj = " << j << "\tk = " << k << endl;
cout << "++k / i = " << ++k / i << endl;
cout << "i = " << i << "\tj = " << j << "\tk = " << k << endl;
cout << "i * j-- = " << i * j-- << endl;
cout << "i = " << i << "\tj = " << j << "\tk = " << k << endl;
Mixed expressions, if valid, generally produce results of the widest of the argument types. [11]
When we execute Example 1.20 will show that the result of a double divided by an int is a double.
Conversions are discussed further in Chapter 19.
C++ also provides a full set of boolean operators to compare numeric expressions.
Each of these operators returns a bool value of either false or true.
less than (<)
less than or equal to (<=)
equal to (==)
not equal to (!=)
greater than (>)
greater than or equal to (>=)
A bool expression can be negated with the unary not (!) operator.
Two bool expressions can be combined with the operators
and (&&)
or (||)
Example 1.21. src/arithmetic/arithmetic.cpp
[ . . . . ]
/* if () ... else approach */
if (x * j <= z)
cout << x * j << " <= " << z << endl ;
else
cout << x * j << " > " << z << endl;
/* conditional operator approach */
cout << x * k
<<( (x * k < y * j) ? " < " : " >= ")
<< y * j << endl;
return 0;
}
In addition to the binary boolean operators, Example 1.21 makes use of the conditional-expression.
(boolExpr) ? expr1 : expr2
returns expr1 if boolExpr is true, and otherwise returns expr2.
Example 1.22 shows the example's output.
Example 1.22. src/arithmetic/arithmetic.cpp
[ . . . . ]Output:
x = 5.79 z = 45.6831 x - z = -39.8931 k / i = 3 k % j = 2 i = 2 j = 5 k = 7 ++k / i = 4 i = 2 j = 5 k = 8 i * j-- = 10 i = 2 j = 4 k = 8 z / j = 11.4208 23.16 <= 45.6831 46.32 >= 18.24
| Generated: $Date: 2009-09-08 12:15:32 -0400 (Tue, 08 Sep 2009) $ | © 2009 Alan Ezust and Paul Ezust. |