next up previous
Next: Example Up: Overview of C++ Previous: Example

Arithmetic Operators

Table 2.5 below lists the arithmetic operators allowed in C++. The operators +, -,* and or algebra for this matter. These can be applied to any built in data type allowed by C++. When / is applied to an integer any remainder will be truncated; for example, 10/3 will equal to 3 in integer division. The modulus operator % also works in C++ the way it does in other languages. Remember that the modulus operation yields the remainder of an integer division. This means that the % cannot be used in type float or double.
 

 
Table 2.5: Arithmetic Operators
Operator Action
- subtraction
+ addition
* multiplication
/ division
% modulus
- decrement
++ increment


Most C++ compilers write very fast, efficient object code for increment and decrement operations that is better than the code generated when the corresponding assignment statement is used. Therefore, it is a good idea to use increment and decrement operators when you can. The precedence of the arithmetic operators is shown in table 2.6

 

 
Table 2.6: Precedence of Operations
Highest ++, -
  *, / %
lowest + -


Operations on the same precedence level are evaluated by the compiler from left to right. Parentheses may be used to later the order of evaluation. When C++ evaluates an expression it always perform operations grouped within parentheses first.

 
next up previous
Next: Example Up: Overview of C++ Previous: Example
Yousef Haik
2/23/1998