Page 30 - DCAP404 _Object Oriented Programming
P. 30
Unit 2: Beginning of OOP Language
3. Unary operators Notes
4. Comparison operators
5. Shift operators
6. Bit-wise operators
7. Logical operators
8. Conditional operators
2.3.1 Arithmetic Operators
There are five arithmetic operators in C++. They are
Operator Function
+ addition
– subtraction
* multiplication
/ division
% remainder after integer division
The % operator is sometimes referred to as the modulus operator.
There is no exponentiation operator in C++. However, there is a library function (pow) to carry
out exponentiation. Alternatively you can write your own function to compute exponential
value.
The operands acted upon by arithmetic operators must represent numeric values. Thus, the
operands can be integer quantities, floating-point quantities or characters (remember that
character constants represent integer values, as determined by the computer’s character set.).
The remainder operator (%) requires that both operands be integers and the second operand be
nonzero. Similarly, the division operator (/) requires that the second operand be nonzero.
Division of one integer quantity by another is referred to as integer division. This operation
always results in a truncated quotient (i.e., the decimal portion of the quotient will be dropped).
On the other hand, if a division operation is carried out with two floating-point numbers, or
with one floating-point number and one integer, the result will be a floating-pointing quotient.
Suppose that a and b are integer variables whose values are 8 and 4, respectively. Several
arithmetic expressions involving these variables are shown below, together with their resulting
values.
Expression Value
a + b 12
a – b 4
a * b 32
a / b 2
a % b 0
LOVELY PROFESSIONAL UNIVERSITY 23