Module 2 Operators
Module 2 Operators
RODERICK M. MASIRAG
OBJECTIVES
• Recognize the diff erent types of
operators
• Evaluate expressions
• Create programs using the diff erent
operators
Note:
• If both are int, if / was used, gives a whole number instead of float
int x =3; y =4
x=4
Postfix example y=3
int x =3; assign the value of x to y, then
increments
int y = x++;
PREPARED BY: RODERICK MASIRAG, MIT
Relational
Operators
Sample data:
C = A + B -> value of C: 30
C += A -> value of C: 40
C -= A -> value of C: 30
C /= A -> value of C: 30
PREPARED BY: RODERICK MASIRAG, MIT
Operator Precedence
Set of rules which defines how an expression is
evaluated
In C#, each operator has an assigned
priority and based on these priorities, the
expression is evaluated
Certain operators have higher precedence
than others
PREPARED BY: RODERICK MASIRAG, MIT
Operator Precedence
Example: x = 7 + 3 * 2;
here, x will have a value of 13, not 20 because
operator * has higher precedence than +, so it first
gets multiplied with 3*2 and then adds into 7.
Remember: PEMDAS rule - Parentheses, Exponents,
Multiplication and Division (from left to right), Addition and
Subtraction (from left to right).
PREPARED BY: RODERICK MASIRAG, MIT
Operator Precedence
1. 19
2. True
1. --a * b - ++c 3. 53
2. b >= c || a<b
3. --b + 8 * ++a
PREPARED BY: RODERICK MASIRAG, MIT
Solution:
1. --a * b - ++c
--5 * 6 - ++4
4*6-5
24 - 5
= 19