Sie sind auf Seite 1von 29

Arithmetic

Operators

CC102 - FUNDAMENTALS OF PROGRAMMING


LESSON IV – Arithmetic 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

CC102 - FUNDAMENTALS OF PROGRAMMING


2
LESSON IV – Arithmetic Operators

Arithmetic Operators Supported by Java

CC102 - FUNDAMENTALS OF PROGRAMMING


3
LESSON IV – Arithmetic Operators

Precedence
table

CC102 - FUNDAMENTALS OF PROGRAMMING


4
LESSON IV – Arithmetic Operators

Precedence determines order of


evaluation
• Mathematical tradition, which programming
languages generally try to match, dictates that
some operations are done before others (for
example, multiplication and division are done
before addition and subtraction).

a+b*c is the same as a+(b*c), BUT NOT (a+b)*c.

CC102 - FUNDAMENTALS OF PROGRAMMING


5
LESSON IV – Arithmetic Operators

The addition and subtraction


operation + and -
• perform arithmetic addition and subtraction
• If the result overflows, the truncation of bits
happens the same way as in multiplication
• The + operator is overloaded in the Java
language to concatenate strings

CC102 - FUNDAMENTALS OF PROGRAMMING


6
LESSON IV – Arithmetic Operators

The modulo Operator


• gives the value that is the remainder of a division
• The sign of the result is always the sign of the
first (from the left) operand

CC102 - FUNDAMENTALS OF PROGRAMMING


7
LESSON IV – Arithmetic Operators

Equal precedence operations generally


performed left-to-right
• In addition to the precedence of each operator,
the compiler also knows whether equal-
precedence operators should be performed:
1. left-to-right (almost all) 2
2. right-to-left (basically only assignment).

CC102 - FUNDAMENTALS OF PROGRAMMING


8
LESSON IV – Arithmetic Operators

Parentheses can be used to control the


order of evaluation
• If you have any doubt about the order of evaluation,
or have a potentially confusing expression, use
parentheses.
• Remember that one of your goals should be to make
your programs as readable as possible.
• Use parentheses when it makes an expression easier
to read, not must when they are absolutely required.
Few programmers know the precedence of all
operators, so it's common for extra parentheses to be
used.
CC102 - FUNDAMENTALS OF PROGRAMMING
9
LESSON IV – Arithmetic Operators

Unary (one operand) vs binary (two


operand) operators
• Unary operators have only one operand, for example, in
a = -b;
The "-" operator is the unary (one operand) operator which
changes the sign of its operand.

a=b-c
The "-" operator is a binary (two operand) operator which
subtracts c from b.

CC102 - FUNDAMENTALS OF PROGRAMMING


10
LESSON IV – Arithmetic Operators

Unary (one operand) vs binary (two


operand) operators
a=b-c
The "-" operator is a binary (two operand) operator which
subtracts c from b.

Most unary operators are performed before binary operators


exceptions: "." (qualification),
"[]" (subscription),
"()" (method call).

CC102 - FUNDAMENTALS OF PROGRAMMING


11
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:

x= 1 + 2 - 3 * 4 / 5

CC102 - FUNDAMENTALS OF PROGRAMMING


12
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)

CC102 - FUNDAMENTALS OF PROGRAMMING


13
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)

CC102 - FUNDAMENTALS OF PROGRAMMING


14
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)

CC102 - FUNDAMENTALS OF PROGRAMMING


15
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?
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

Alternative notation - Dataflow diagram


Let's look at
the expression:
x = a+b-c*d/e,
which can be
parenthesized as:
x = ((a+b) - ((c*d)/e)).
Instead of parentheses,
we can draw a diagram.

CC102 - FUNDAMENTALS OF PROGRAMMING


18
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

Arithmetic Promotion Rules


• If one operand is of type "double", then the other
operand is promoted to the type double.
• If Both operands are of type less than "int" , for
example (byte,short,char...) .Both operands are
promoted to type integer.
• If one of operands is type float and the other is not
double .The other operand is converted to type
float.

CC102 - FUNDAMENTALS OF PROGRAMMING


20
LESSON IV – Arithmetic Operators

Arithmetic Promotion Rules


• If one of operands is type long and the other is not
double or float .The other operand is converted to
type long.
• If no operand of type (double,float ,long ) , then
convert to type integer .

CC102 - FUNDAMENTALS OF PROGRAMMING


21
LESSON IV – Arithmetic Operators

Arithmetic Promotion Rules:


• If one of operands is type long and the other is not
double or float .The other operand is converted to
type long.
• If no operand of type (double,float ,long ) , then
convert to type integer .
Note : All arithmetic promotion is done before calculations .
The result return type will be the common type created after the
arithmetic promotion.
Note : Any arithmetic operations will be at least of type integer (int)

CC102 - FUNDAMENTALS OF PROGRAMMING


22
LESSON IV – Arithmetic Operators

Arithmetic Promotion Rules Example:


byte a=2;
byte b=4;

the result of a*b or a/b will be of type integer .

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.

CC102 - FUNDAMENTALS OF PROGRAMMING


23
LESSON IV – Arithmetic Operators

Relational Operators
• also called a comparison operator, compares
the values of two operands and returns a
boolean value: true or false

CC102 - FUNDAMENTALS OF PROGRAMMING


24
LESSON IV – Arithmetic Operators

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

two kinds of logical operators:


1. bitwise logical operators
2. short-circuit logical operators

CC102 - FUNDAMENTALS OF PROGRAMMING


25
LESSON IV – Arithmetic Operators

Bitwise Logical Operators


• manipulate the bits of an integer (byte, short,
char, int, long) value

CC102 - FUNDAMENTALS OF PROGRAMMING


26
LESSON IV – Arithmetic Operators

Bitwise Logical Operators Example:

TEST THIS:

CC102 - FUNDAMENTALS OF PROGRAMMING


27
LESSON IV – Arithmetic Operators

Short Circuit Logical Operators


• operate on the boolean types
• The outcome of these operators is a boolean

CC102 - FUNDAMENTALS OF PROGRAMMING


28
LESSON IV – Arithmetic Operators

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

Das könnte Ihnen auch gefallen