S5 2 Operators
S5 2 Operators
Learning Objectives
To learn and appreciate the following concepts
− Type conversions
− Assignment Operators and Conditional Expressions
− Precedence and Order of Evaluation
− Type conversions
− Assignment Operators and Conditional Expressions
− Precedence and Order of Evaluation
• The type cast operator has the effect of converting the value of the variable ‘a’ to
type float for the purpose of evaluation of the expression.
• This operator does NOT permanently affect the value of the variable ‘a’;
• The type cast operator has a higher precedence than all the arithmetic operators
except the unary minus and unary plus.
Example Action
x=(int) 7.5 7.5 is converted to integer by
truncation
a=(int) 21.3/(int)4.5 Evaluated as 21/4 and the result
would be 5
b=(double)sum/n Division is done in floating point mode
y=(int)(a+b) The result of a+b is converted to
integer
z=(int)a+b a is converted to integer and then
added to b
p=cos((double)x) Converts x to double before using it
Equivalent to:
if ( a > b )
maxValue = a;
else
11/3/2021 maxValue = b; CSE 1051 Department of CSE 14
Comma (,)operator
▪ The coma operator is used basically to separate expressions.
i = 0, j = 10; // in initialization [ l → r ]
Detailed
Precedence
Table
2*((i/5)+(4*(j-3))%(i+j-2))
18
11/3/2021 CSE 1051 Department of CSE
Example solution:
2*((8/5)+(4*(5-3))%(8+5-2))
2*(1+(4*2)%11)
2*(1+8%11)
2*(1+8)
2*9
18
Evaluation:
+ (x==25 && y< 10)
< (x==25 && true)
== (False && true)
&& (False)
− Arithmetic Operators
− Relational and Logical Operators
− Increment and Decrement Operators
− Bitwise Operators
− Type conversions
− Assignment Operators and Conditional Expressions
− Precedence and Order of Evaluation