Topic 2 - Part 2
Topic 2 - Part 2
ARITHMETIC OPERATIONS
TYPE CONVERSION
+ Addition 4+2 6
- Subtraction 4-2 2
* Multiplication 4*2 8
/ Division 4/2 2
% Modulus 5%2 1
(Remainder)
ARITHMETIC OPERATIONS (CONT.)
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
a.
b.
c.
ORDER OF PRECEDENCE (CONT.)
float x = 1.5;
int y = 3, z = 7;
cout << u;
float v = z / x + 1;
cout << v;
VARIATION OF ASSIGNMENT STATEMENT
+= 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
Example:
ASSIGNMENT STATEMENT (CONT.)
Exercises