Operators
Kinds of Operators Precedence of Operators
Classifications
Arithmetic operators Relational operators Logical operators Assignment operators Increment and decrement operators Conditional operators Bitwise operators Special operators
Kinds of Operator
Arithmetic Op. : + * / % Relational Op. : > >= < <= == != Logical Op. : && || ! Inc/Dec Op. : ++ -Bit Op. : & | ^ ~ << >> >>> Conditional Op. : ?: Assign Op. : = += -= *= /= %= &= ^= |= >>= <<= >>>= Casting Op. : (Data Type) Array Op. : [ ] Method Op. : () . instanceof Op. : instanceof
Operators
Arithmetic Operators
Operator for arithmetic operation
operators : +, -, *, /, %
a-b a*b a+b a%b -a*b a/b
7/4 9.0 9/4 4/7 =1; 4%7 = 4; / 2= 4.5; 7%4 = 3; =2; 9/2.0 = 4.5; = 0; 9.0/2.0 = 4.5;
% is purely integer operator.
9.0 % 2; 9.0 % 2; 9%2.0; 9%2.0; 9.0%2.0; 9.0%2.0;
Integer arithmetic
Note: In modulo division, the sign of the result is always the sign of the first operand (dividend).
6/3 = 2 -6/3 = -2 6/-3 = -2 -6/-3 = 2 7%3 = 1 -7%3 = -1 7%-3 = 1 -7%-3 = -1
Precedence of arithmetic operators
High priority * / % Low priority + Left to right evaluation
Order of evaluation can be changed by introducing parentheses Ex: 1.a = 9, b = 12, c = 3 x = a b/(3 + c)*(2 1) For nested parentheses, inner most parentheses will be first evaluated. Ex: x = 9 ((12/3) + 3*2) 1
Precedence of arithmetic operators
Relational Operators
Compare two value. Result : true (1)or false (0). Zero is false, non zero integer is true.
Relational Operators
7>4 = 1 7>4 Examples: = 1 7>9 = 0; 7>9 = 0; 7<9 = 1; 7<9 = 1; 7 == 7 = 1; 7 == 7 = 1; 7 == 9 =0; 7 == 9 =0; 7 !=7 =0; 7 !=7 =0; 10<7+5 10<7+5 a+b == c+d a+b == c+d
Relational Operators
true true true if a+b is equal to c+d true if a+b is equal to c+d
7>=4 = 1; 7>=7 = 1; 7>=4 = 1; 7>=7 = 1; 9<=7 = 0; if at least one condition is true ,, 9<=7 = 0; if at least one condition is true result is true. result is true.
Logical Operators
Operators && ! || Logical AND Logical NOT Logical OR
a < b && b < c
An expression which combines two or more relational expressions is called as logical expression or compound relational expression.
Logical Operators Truth table
Op1 Non zero Non zero 0 0 Op2 Non zero 0 Non zero 0 Op1 && Op2 1 0 0 0 Op1 || Op2 1 1 1 0
Assignment Operators
Expr1 = Expr1 op Expr2 Expr1 = Expr1 op Expr2 Expr1 op= Expr 2 Expr1 op= Expr 2
Ex:
sum = sum + ii;; sum = sum + xx= xx**yy+ 1; = + 1;
sum += ii;; sum += xx*= yy+ 1; *= + 1;
x = x * (y+1) x = x * (y+1)
Increment & Decrement Operators
Operator ++, -Prefix operator (evaluation and then nn= 1; assignment) = 1;
xx= ++n; = ++n; // x=2, n=2 // x=2, n=2
Postfix operator (assignment and then // x=1, n=2 // x=1, n=2 evaluation)
(a + b)++ // error (a + b)++ // error
nn= 1; = 1; xx= n++; = n++;
Cannot use at expression, only at variable
Increment & Decrement Operators
Examples: => a[i] 1.a[i++] = 10; = 10; i = i+1 2.m = n++ -j+10;
The Conditional Operator
Expr1 ? Expr2 : Expr3 (3 Terms if (x > y) Operator) if (x > y)
max = xx> yy??xx::yy;; max = >
max = x; max = x; else else max = y; max = y;
m = aa> bb??(c > aa??cc::a) ::(c > bb??cc::b) ;; m= > (c > a) (c > b)
if (a > b) if (a > b) then if(c>a) then if(c>a) m = c; m = c; else m=a; else m=a;
if (a > b) if (a > b) then if(c>b) then if(c>b) m = c; m = c; else m=b; else m=b;
Bitwise Operators
Operand should be integer type
Bitwise Operators
Precedence
Operator Precedence
(H)
~ << >> & ^ |
(L)
Bitwise Operators
Bitwise AND
10012 & 00112 = 00012 To extract the special area in variable by masking that area
Bit OR
10012 | 00112 = 10112
Exclusive OR
10012 ^ 00112 = 10102
1s Complement
~ 000010102 = 111101012
Bitwise Operators
Bitwise Shift Operator
Shift left(<<)
x << y = x **2yy x << y = x 2
Shift right(>>)
x >> y = x //2yy x >> y = x 2
Special operators
1. The Comma operator: used to link the related expressions together. Evaluated left to right has lowest precedence, parentheses are necessary. Ex: value = (x=10,y=5,x+y);
Special operators
2. The sizeof operator: It returns the number of bytes the operand occupies. the operand can be a variable, constant or a data type qualifier. Ex: m = sizeof(sum); n = sizeof(long int); k = sizeof(235L);
Operator Precedence
Operator Association
Left Assoc. () [] . (High) ! ~ ++ -- + - (Data Type) Left Assoc. * / % Left Assoc. + Left Assoc. << >> >>> Left Assoc. < <= > >= instance Left Assoc. Left Assoc. == != Left Assoc. & Left Assoc. ^ Left Assoc. | Left Assoc. && Left Assoc. || Left Assoc. ?: Left Assoc. = += -= *= /= %= &= ^= |= <<= >>= >>>= (Low)
Precedenc
Operator Precedence
a = x + y - z ; // Left Association b = -x ; // Right Association c = -x++ ; d = -++x ; e = -x + z ;
Operator Precedence
Example: 3 + 4*5 6/2 + 7*8 3 + 20 - 6/2 + 7*8 3 + 20 - 3 + 7*8 3 + 20 - 3 + 56 23 -3 +56 20 + 56 76
Operators
Determine the hierarchy of operations and evaluate the following expression: i=2*3/4+4/4+8-2+ 5/8
Operators
i=6/4+4/4+8-2+5/8 operation: * i=1+4/4+8-2+5/8 operation: / L-R i = 1 + 1+ 8 - 2 + 5 / 8 operation: / L-R i=1+1+8-2+0 operation: / L-R i=2+8-2+0 operation: +
Algebraic Expression axbcxd (m + n) (a + b) 3x2 + 2x + 5 (a+b+c)/(d+e) 2by d+1 -x 3(z+y)
C-Expression a*bc*d (m + n) * (a + b) 3*x*x+2*x+5 (a+b+c)/(d+e) 2*b*y/(d+1)x/3*(z +y)