Chapter 3
Chapter 3
Chapter 3
Lecture Outline
• What operators are
• Arithmetic Operators such as +, -
• Assignment Operator
• Increment/Decrement Operators e.g i++
• Relational Operators
• Conditional Operators
2
What are Operators?
• Operators are special symbols used for:
- mathematical functions
- assignment statements
- logical comparisons
• Examples of operators:
3+5 // uses + operator
14 + 5 – 4 * (5 – 3) // uses +, -, *
operators
• Expressions can be combinations of variables,
primitives and operators that result in a value
3
The Operator Groups
• There are 5 different groups of
operators:
- Arithmetic Operators
- Assignment Operator
- Increment / Decrement
Operators
- Relational Operators
- Conditional Operators
6
Order of Operations (Cont’d)
• Example: 10 + 15 / 5;
7
Integer Division
• In the previous example, we were lucky
that (10 + 15) / 5 gives an exact
integer answer (5).
8
Integer Division (Cont’d)
• int i = 63;
= 35;
int j
System.out.println(i / j);
Output: 1
• double x = 63;
double y = 35;
System.out.println(x / y);
Ouput: 1.8
9
Assignment Operator
• The basic assignment operator (=) assigns
the value of expr to var
var = expr ;
• Examples:
x = x + 5; is equivalent to x += 5;
y = y * 7; is equivalent to y *= 7;
10
Increment/Decrement Operators
• ++ is called the increment operator. It is used
to increase the value of a variable by 1.
For example:
i = i + 1; can be written as:
++i; or i++;
11
Increment Operators (cont’d)
• The increment / decrement operator has
two forms :
12
Prefix increment /decrement
• Example:
int numOranges = 5;
int numApples =
10; int numFruit;
numFruit = +
+numOranges +
numApples;
13
Postfix Increment/ Decrement
• The postfix form i++, i-- first evaluates the
entire expression and then adds 1 to the
variable
• Example:
int numOranges = 5;
int numApples =
10; int numFruit;
numFruit =
numOranges++ +
numApples;
15
Examples of Relational Operations
int x = 3;
int y = 5;
boolean
result;
1) result =
(x > y);
now result is assigned the value false because 3
is not greater than 5
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)
16
Conditional Operators
Symbol Name
&& AND
|| OR
! NOT
17
Truth Table for Conditional Operators
x y x && y x || y !x
18
Examples of Conditional Operators
boolean x = true;
boolean y = false;
boolean result;
1. Let result =
(x && y);
now result is
assigned the
value false
(x table!)
(see truth || y) evaluates to true
2. (true &&
Let result = x) evaluates to true
((x || y) &&
now result is assigned the value
x);
true
19
Using && and ||
• Java performs short-circuit evaluation: By
this we mean that it evaluates && and ||
expressions from left to right and once it finds
the result, it stops.
• Examples:
(a && (b++ > 3))
(x || y)
20
Short-Circuit Evaluation
(a && (b++ > 3));
What happens if a is false?
• Java will not evaluate the right-hand expression (b++
> 3) if the left-hand operator a is false, since the
result is already determined in this case to be false.
This means b will not be incremented!
(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.
21
POP QUIZ
1) What is the value of number?
int number = 5 * 3 – 3 / 6 – 9 * 3;
2)What is the value of result?
int x = 8;
int y = 2;
boolean result = (15 == x *
y);
3)What is the value of result?
boolean result
x = 7; = (x < 8) && (x > 4);
4) What is the value of numCars?
int numBlueCars = 5;
int numGreenCars = 10;
int numCars = numGreenCars++ +
numBlueCars + ++numGreeenCars;
22
POP Quiz Solutions
1) What is the value of number? -12
int number = 5 * 3 – 3 / 6 – 9 * 3;
2)What is the value of result? false
int x = 8;
int y = 2;
boolean result = (15 == x * y);
24
This Lecture Covered….
• What Operators are
• The different types of operators
• The order of Operations for arithmetic
operators
• Prefix and Postfix operators
• Short Circuit Evaluation
25