STD 9 Java - pptl5
STD 9 Java - pptl5
a is an operand
+ is an operator
b is an operand
* is an operator
4 is a constant
CHAPTER - 5
Operators
Forms of Arithmetic
Operators
Unary (+) +, - , *, /, % ?:
Unary (-)
Unary increment (++)
Unary decrement (--)
Unary Operators
Eg : +, -, ++, --
unary (+) is applied before the operand
i.e:
if a = 8 then + a will result in 8
If b= -16 then b store a value -16
Example:
Example:
3. If c =2, find
d= ++c + c++ +4;
Unary Operators
1. if p=5, find d 4. if m=12 find n
2. If a= 48, find b
1+1 5. int a =0, b=30,c=40;
b = a++ + ++a; a= --b + c ++ + b
= 48 + 50 = 29+ 40 +29
=98 = 98
3. If c =2, find
d= ++c + c++ +4;
= 3 + 3 +4
= 10
Binary Operators
• Binary operators are used to calculate.
1. p=abc
2. R=2(l+b)
3. S=ab + bc + ca
4. 2(lb+bh+lh)
Expressions
1. p=a*b*c
2. R=2*(l+b)
3. S=a*b + b*c + c*a
4. 2*(l*b+b*h+l*h)
Shorthand Operator
• Shorthand operators are used to write an expression in a short
form for example.
• Example :
x=2 y=5
if (x > 5)
System.out.println( “x is greater”);
else
System.out.println( “x is smaller”);
Logical Operators
• Logical operators are used to combine two or more
expressions.
• Logical operators returns only boolean value.
• Logical operators are:
&& ( AND)
|| ( OR)
! ( NOT)
Example:
x=2 y =5
If (x>=y) &&(x==y) returns the value as
= (2>=5) && (2==5)
= F && F
= F
Truth table for AND and OR
AND OR
X Y X&&Y X Y X||Y
T T T T T T
T F F T F T
F T F F T T
F F F F F F
x=2 y =5
If (x>=y) &&(x==y) returns the
value as
= (2>=5) && (2==5)
= F && F
= F
Truth table for NOT
NOT
X !X
T F
F T
x=2 y =5
If !(x>=y) &&!(x==y) returns the value as
= !(2>=5) &&! (2==5)
= !(F) && !(F)
= T &&T
= T
DIFFERENCE BETWEEN BINARY THE OPERATORS
ARITHMETIC RELATIONAL LOGICAL
int g = (a>b) ? a : b;
Rewrite the if statement into its
equivalent Ternary operator
1) if (marks >=90)
grade =“A”;
else
grade =“B”;
2) if (x%2 ==0)
ans= “even”;
else
ans = “odd”;
4) if (p>5000)
d= p*5/100;
else
d= p*2/100;
import java.io.*;
all the functions in the package
dot
operator
System.out.println(); System.out.print()
object
Class function
Different forms of operator
Hierarchy of operators
Hierarchy of
Operators