Lecture -3
Lecture -3
Faculty of Technology
Department of Electrical and Computer
Engineering
• Examples:
3 + 5 // uses + operator
14 + 5 – 4 * (5 – 3) // uses +, -, * operators
– Arithmetic operators
– Assignment operator
– Increment/Decrement operators
– Relational operators
– Conditional operators
Arithmetic Operators
• double x = 63;
double y = 35;
System.out.println(x / y);
Ouput: 1.8
count = count - 1;
can be written as:
--count; or count--;
int numOranges = 5;
int numApples = 10;
int numFruit;
numFruit = ++numOranges + numApples;
numFruit has value 16
numOranges has value 6
int numOranges = 5;
int numApples = 10;
int numFruit;
numFruit = numOranges++ + numApples;
numFruit has value 15
numOranges has value 6
Relational (Comparison) Operators
• Relational operators compare two values
• Produces a boolean value (true or false)
depending on the relationship
int x = 3;
int y = 5;
boolean result;
3) result = (x != x*y);
now result is assigned the value true because the product of
x and y (15) is not equal to x (3)
Conditional Operators
Symbol Name
&& AND
|| OR
! NOT
(x || y) evaluates to true
(true && x) evaluates to true
• Examples:
(a && (b++ > 3))
(x || y)
(x || y)
What happens if x is true?
• Similarly, Java will not evaluate the right-hand operator
y if the left-hand operator x is true, since the result is
already determined in this case to be true.
POP QUIZ
1) What is the value of number? -12
int number = 5 * 3 – 3 / 6 – 9 * 3;