TOPIC 2 (PART II):
ARITHMETIC OPERATIONS
TYPE CONVERSION
Type conversion or typecasting refers to changing an entity of one
datatype into another
It is used in computer programming to ensure variables are correctly
processed by a function.
Expressed using static_cast keyword as follows :
static_cast <data_type> (expression)
TYPE CONVERSION (CONT.)
ARITHMETIC OPERATORS
Operator Definition Example Output
+ Addition 4+2 6
- Subtraction 4-2 2
* Multiplication 4*2 8
/ Division 4/2 2
% Modulus (Remainder) 5%2 1
ARITHMETIC OPERATIONS (CONT.)
+, -, * and / operation can be performed on integer numbers as well as
floating numbers
Remainder operation can only be performed on integer numbers
Floating point operands are not accepted by remainder operator
Unary operator – has only one operand
Binary operator – has two operand
OPERATORS AND OPERANDS
Almost all of the program use some sort of data stored in variables
In a program different types of operations are performed on that data
The data on which operations are performed are knows as operands and
the types of the operations performed are known as operators
Example:
Operator
A+B
Operands
ORDER OF PRECEDENCE
All operations inside of ( ) are evaluate
*, /, and % are at the same level of precedence and are evaluated
next
+ and – have the same level of precedence and are evaluated last
When operators are on the same level
Performed from left to right
ORDER OF PRECEDENCE (CONT.)
Exercises: Rewrite the following statements in C++ format.
a.
b.
c.
ORDER OF PRECEDENCE (CONT.)
Given the following variable declarations:
float x = 1.5;
int y = 3, z = 7;
Determine the output of the following statement:
int t = y * z + z * (6 – y);
cout << t;
int u = (y + z) % 3 + 12 / y + 6;
cout << u;
float v = z / x + 1;
cout << v;
VARIATION OF ASSIGNMENT STATEMENT
Operation Description Example Equivalent to
+= Addition Assignment a += b a = a + b
-= Subtraction Assignment a -= b a = a - b
/= Division Assignment a /= b a = a / b
*= Multiplication Assignment a *= b a = a * b
%= Modulus Assignment a %= b a = a % b
VARIATION OF ASSIGNMENT STATEMENT
(CONT.)
Example:
Expression Equivalent to
price = price * rate; price *= rate;
count = count + 3; count += 3;
amount = amount / 2; amount /= 2;
price *= rate + 1; ??
ASSIGNMENT STATEMENT
For a variable to increase or decrease by 1, C++ provides two unary
operators:
( ++ ) : increment operator
( -- ) : decrement operator
Prefix ++i; Increment the value of i (i = i + 1), then use it
--i; Decrement the value of i (i = i – 1), then use it
Suffix i++; Use the value of i, then increment (i = i + 1) it
i--; Use the value of i, then decrement (i = i - 1) it
ASSIGNMENT STATEMENT (CONT.)
Example:
ASSIGNMENT STATEMENT (CONT.)
Exercises
Expression Value X Value Y
x = 6;
y = ++x;
x=10;
y=x++;
x = 5;
y = --x;
x = 10;
y = x--;
MATHEMATICAL LIBRARY FUNCTIONS
Perform compute common mathematical operations and transformations
Include the header file <cmath>
Example :
MATHEMATICAL LIBRARY FUNCTIONS (CONT.)
Exercises: Write C++ assignment for the following equations.