Вы находитесь на странице: 1из 14

O P E R AT O R S

P R E PA R E D B Y : M S . M A R I C H U A . B A U T I S TA
ARITHMETIC OPERATIONS
• an evaluation of an arithmetic expression.
• these are also called binary operators, which
requires two operands to produce a result.
OPERATION OPERATOR
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus Division
ARITHMETIC OPERATIONS
• Arithmetic operations in C++ must be entered
in a straight-line form.
Algebraic Expression C++ Expression
12.6 12.6 / 2
2
INTEGER DIVISION /
• Integers can’t contain fractional part, in dividing
the remainder is always dropped.
Therefore, the value 9/4 is 2
MODULO %
• modulus operator gives the remainder of an
integer division.
Therefore: 9%4 is 1
OPERATOR PRECEDENCE AND
ASSOCIATIVITY
• When writing complex expressions with several
operands, there are rules to follow in evaluating
the expressions.
Ex. a=5+7%2
What is the answer?
The answer is 6.
The modulus operator is evaluated first.
RULES ON PRECEDENCE AND
ASSOCIATIVITY
1. Two binary operators must never be placed
side by side. Ex. 5 * % 6
2. Parentheses can be used to form groupings,
and all expressions enclosed in parentheses
are evaluated first.
Ex. 6 + 4 / 2 + 3 is 11
(6+4) / (2+3) is 2
RULES ON PRECEDENCE AND
ASSOCIATIVITY
3. Parentheses can be enclosed by other
parentheses.
Ex. (12 * (3 + 7) ) / 5 is 24
(25 * (8 – 3) ) / (2 * ( 2 + 3) ) is ??
Expressions in the innermost parentheses
are evaluated first. The rule is to evaluate the
innermost to outermost parentheses.
RULES ON PRECEDENCE AND
ASSOCIATIVITY
4. Parentheses can’t be used to indicate
multiplication.
Ex. ( 3 + 4 ) ( 5 + 1)
instead ( 3 + 4 ) * ( 5 + 1 )
RULES ON PRECEDENCE AND
ASSOCIATIVITY
5. Multiplication, Division and Modulus
operations are computed first.

Expressions containing more than one


multiplication, division or modulus operator are
evaluated from left to right as each operator is
encountered.
RULES ON PRECEDENCE AND
ASSOCIATIVITY
Example: 35 / 7 % 3 * 4 is ____
5%3*4
2 * 4 is 8
RULES ON PRECEDENCE AND
ASSOCIATIVITY
6. Addition and Subtraction are computed last.

Expressions containing more than one addition


and subtraction are evaluated from left to right
as each operator is encountered.
RULES ON PRECEDENCE AND
ASSOCIATIVITY
SUMMARY

OPERATOR ASSOCIATIVITY
* / % Left to Right
+ - Left to Right
Operations inside parentheses are always
evaluated first.
RULES ON PRECEDENCE AND
ASSOCIATIVITY
Example:

8+5*7%2*4 is 12

Вам также может понравиться