Beruflich Dokumente
Kultur Dokumente
Operators
LEARNING OUTCOMES:
At the end of the session, the students should be
able to:
• Analyse and explain the behaviour of simple
programs involving the fundamental programming
constructs in JAVA
• Implement correct operational hierarchy
Precedence
table
a=b-c
The "-" operator is a binary (two operand) operator which
subtracts c from b.
Example - Parentheses
When you can work out the precedence, it's often
useful to use parentheses to figure out the order of
evaluation. For example:
x= 1 + 2 - 3 * 4 / 5
Example - Parentheses
When you can work out the precedence, it's often
useful to use parentheses to figure out the order of
evaluation. For example:
int x;
x= 1 + 2 - 3 * 4 / 5
x= (1 + 2) - ((3 * 4) / 5)
Example - Parentheses
When you can work out the precedence, it's often
useful to use parentheses to figure out the order of
evaluation. For example:
int x;
x= 1 + 2 - 3 * 4 / 5
x= (1 + 2) - ((3 * 4) / 5)
x= 3 - (12/5)
Example - Parentheses
When you can work out the precedence, it's often
useful to use parentheses to figure out the order of
evaluation. For example:
int x;
x= 1 + 2 - 3 * 4 / 5
x= (1 + 2) - ((3 * 4) / 5)
x= 3 - (12/5)
Example - Parentheses
When you can work out the precedence, it's often
useful to use parentheses to figure out the order of
evaluation. For example:
int x;
x= 1 + 2 - 3 * 4 / 5
x= (1 + 2) - ((3 * 4) / 5)
x= 3 - (12/5)
x= 3 – 2 ???? Why 12÷5 is 2?
CC102 - FUNDAMENTALS OF PROGRAMMING
16
LESSON IV – Arithmetic Operators
Example - Parentheses
When you can work out the precedence, it's often
useful to use parentheses to figure out the order of
evaluation. For example:
int x;
x= 1 + 2 - 3 * 4 / 5
x= (1 + 2) - ((3 * 4) / 5)
x= 3 - (12/5)
x= 3 – 2 ???? Why 12÷5 is 2? Therefore x=1
CC102 - FUNDAMENTALS OF PROGRAMMING
17
LESSON IV – Arithmetic Operators
Arithmetic Promotion
• involves binary operation between two operands
of different types or of types narrower in size
than int
• the compiler may convert the type of one
operand to the type of the other operand, or
the types of both operands to entirely
different types
• Arithmetic promotion is performed before any
calculation is done
CC102 - FUNDAMENTALS OF PROGRAMMING
19
LESSON IV – Arithmetic Operators
Last thing you must take in mind that you can't convert any
type , there will be a situations you will need to use the cast
operator <type> of java.
Relational Operators
• also called a comparison operator, compares
the values of two operands and returns a
boolean value: true or false
Logical Operators
• used to combine more than one condition that
may be true or false
• deal with connecting the boolean values
• operate at bit level
TEST THIS:
class ArithmeticDemo {
Hands On public static void main (String[] args) {
Activity int result = 1 + 2;
System.out.println("1 + 2 = " + result);
•Determine the int original_result = result;
output of this result = result - 1;
System.out.println(original_result +" - 1 = "+ result);
program: original_result = result;
•Write your result = result * 2;
program output System.out.println(original_result +" * 2 = "+ result);
original_result = result;
on a yellow
result = result / 2;
paper. System.out.println(original_result +" / 2 = "+ result);
•Test the original_result = result;
program on the result = result + 8;
System.out.println(original_result +" + 8 = "+ result);
laboratory to original_result = result;
determine result = result % 7;
the correct System.out.println(original_result +" % 7 = "+ result);
}
output
} 29
CC102 - FUNDAMENTALS OF PROGRAMMING