You are on page 1of 3

Arithmetic and Relational Operators

What are operators?

• Operators are needed to transform or manipulate data

• Mathematical computations, comparison of two values or setting initial values of


variables are made possible by using arithmetic or relational operators

• Operators are important in that a miscalculation, missed value or wrong comparison


may compromise the integrity of a whole module or a whole program

• The two types of operators in this section are:

– Arithmetic operators
– Relational operators

• An arithmetic or logical expression is formed by a combination of variables or literals


and an operator

• Format for an arithmetic or logical expression:

< operand1 > operator < operand2 >

where operand1 or operand2 could be any literal or variable name

Arithmetic Operators

• Arithmetic operators perform mathematical calculations on two numeric operands

Operator Description

• + Addition
• - Subtraction
• * Multiplication
• / Division
• % Remainder after Division
• Examples of arithmetic expressions:

length * width
12.345 + 67.893 + 75.9004

• Expressions with mixed operands are evaluated using rules of precedence

• Rules of precedence dictate the order of evaluation. In this order, certain parts of the
expression gets executed first before others

• For arithmetic operators, the rules of precedence (or order of evaluation) are:

1. Expressions in parentheses
2. Multiplication and Division, from left to right
3. Addition and subtraction, from left to right

• The assignment operator “=“ has the lowest precedence

• In the expression,

answer = 4 + 6 * 2

the result of the expression would be 16.

• However, if it were modified to:

answer = (4 + 6) * 2

the result of the expression would be 20.

This value would then be assigned to the variable answer.

Increment and Decrement Operators

• Java also has unary increment and decrement operators that increase or decrease the
value of a variable by 1

o Increment operator

< operand1 > ++

o Decrement operator

< operand1 > --


where operand1 could be any literal or variable name

• Example:

the expression,
count = count + 1;
could be written as
count ++;

• Increment and Decrement operators could be postfix or prefix as shown below

• ++ (op++) the value of op was evaluated before it was incremented


• ++ (++op) the value of op was evaluated after it was incremented
• -- (op--) the value of op was evaluated before it was incremented
• -- (--op) the value of op was evaluated after it was incremented

Relational Operators

• Relational or conditional operators perform comparison of two literals or two variables,


or any combination of both.

• The evaluation of a conditional expression results in a boolean value of either true


or false

• Example: the expression: age > 18

will evaluate to only one value: true or false

• == (Equal to) Evaluates as true if its operands are equivalent


• > (Greater than) Evaluates as true when the left operand is greater than the right
operand
• < (Less than) Evaluates as true when the left operand is less than the right
operand
• >= (Greater than or equal to) Evaluates as true when the left operand is greater
than or equal to the right operand
• <= (Less than or equal to) Evaluates as true when the left operand is less than or
equal to the right operand
• != (Not equal to) Evaluates as true when both operands are not equal

You might also like