Operator
Operator
• Arithmetic Operator
• Unary operators
• Relational and logical operators
• Assignment operators and
• Conditional operator
a-b 7
a*b 30
a-b 10.5
a*b 25.0
a/b 6.25
a+b 131
a+1 66
a + ‘A’ 130
a + ‘1’ 114
int a, b;
a = 11;
b = -3;
Follow
basic
rules of
a+b 8 algebra
a-b 14
a*b -33
a/b -3
• Operands that differ in type may undergo type conversion before the
expression takes on its final value.
• In general, the final result will be expressed in the highest precision
possible, consistent with the data types of the operands.
• Rules apply when neither operand is unsigned.
float
Left
int 11.995
Right
float
int
3 float
Left
3.0
int
11
;
Type Cast
int number;
(float) number;
Valid or Invalid?
i = 7;
f = 8.5;
result = (i + f) % 4; Invalid
• Type conversion 2
Valid or Invalid?
num % 2;
((int)num) % 2;
Operators and Operands
• Arithmetic Operator
• Unary operators
• Relational and logical operators
• Assignment operators and
• Conditional operator
Unary Operators
• For equality
Relational Operator
• The relational operators compare two values
and return a true or false result based upon
that comparison.
Operator Meaning Type
4-23
i=1
True or False j=2
k=3
i<j true 1
(1 + j) >= k true 1
(j + k) > (i + 5) false 0
i=1
True or False j=2
k=3
k != 3 false 0
j == 2 true 1
(j + k) >= (i + 5) false 0
Simplified Expression
Logical Operation
• There are three kinds of logical operators.
True or False!
i=7
True or False f = 5.5
c = ‘w’ (119)