[ 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. Optimizing compilers can often execute the prefix increment operators in fewer operations, so they are recommended over the postfix version. Example 1.18 through Example 1.22 demonstrate the use of the C++ arithmetic operators.
Example 1.18. src/arithmetic/arithmetic.cpp
[ . . . . ]
#include <iostream>
int main() {
using namespace std;
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 shows 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 largest of the argument types.
Example 1.20 shows 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;
}
In addition to the binary boolean operators, Example 1.21 makes use of the conditional-expression. The
(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: 2008-06-09 14:08:23 -0400 (Mon, 09 Jun 2008) $ | © 2008 Alan Ezust and Paul Ezust. |