You are on page 1of 24

Lesson 4

BASIC
PERATORS
I T 2 1 3

Object Oriented Programming


OVERVIEW
• What are Operators?
• Types of operators
1. Arithmetic Expression and Operators
2. Unary Operators
3. Relational Operators
4. Logical Operators
5. Assignment Operators

2
What are Operators?

• Operators are used to perform operations on variables and


values.
• Operators are needed to transform or manipulate data
• Mathematical computations, comparison of two values or
setting initial values of variables are made possible.
• Operators are important in that a miscalculation, missed
value or wrong comparison may compromise the integrity of
a whole module or a whole program
Logical
operators

Arithmetic
Assignment
operators
operators

Types of operators

Unary Relational
Operators operators
ARITHMETIC
OPERATORS
5
Arithmetic Operators

• Arithmetic operators perform mathematical calculations on


two numeric operands.

Operators Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Computes the remainder
Arithmetic Operators

• For arithmetic operators, the rules of precedence (or order of


evaluation) are:

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

• The assignment operator “=“ has the lowest precedence


ARITHMETIC EXPRESSION

• 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
ASSIGNMENT
OPERATORS
9
Assignment Operators

• Are operators which are used to assign values to variables.

Operators Operation
+= Addition
-= Subtraction
*= Multiplication
/= Division
%= Computes the remainder
UNARY
OPERATORS
11
UNARY OPERATORS
Unary operators are used with Operator Meaning
only one operand.
Unary plus: not
Example: ++ is a unary operator that +
necessary to use since
increases the value of a variable by 1. numbers are positive
without using it
++5 will return 6.
Unary minus: inverts
INCREMENT and DECREMENT - the sign of an
OPERATORS expression
• Java also has unary increment
++
Increment operator:
and decrement operators that increments value by 1
increase or decrease the value
of a variable by 1 Decrement operator:
--
decrements value by 1
• where operand1 could be any
literal or variable name
INCREMENT

OPERATORS
13
Types of Increment Operators

•Pre-increment- In this case, the value of the variable is


first increased and then its used/ or its result is computed.

Example: ++a;

•Post-increment- In this case the value of the variable is


first computed and then incremented.

Example: a++;
DECREMENT

OPERATORS
15
Types of Decrement Operators

•Pre-decrement- In this case the value of the variable is


first decreased and then computed.

Example: --a;

•Post-decrement- In this case the value of the variable is


first computed and then decremented by 1

Example: a--;
RELATIONAL
OPERATORS
17
RELATIONAL OPERATORS

• Relational or conditional operators perform comparison of two


literals or two variables or it is used to check the relationship between
the two operands.
• The evaluation of a conditional expression results in a boolean value

Example: the expression


age > 18
will evaluate to only one value: true or false
Relational Operators

Symbol Name Description Example

Evaluates as true if its operands 3 == 5 returns


== Is Equal To
are equivalent false

Not Equal To Evaluates as true when both operands are not 3 != 5 returns
!=
equal true

Evaluates as true when the left operand is greater 3 > 5 returns


> Greater Than
than the right operand false

Evaluates as true when the left operand is less 3 < 5 returns


< Less Than
than the right operand true

Greater Than Evaluates as true when the left operand is greater 3 >= 5 returns
>=
or Equal To than or equal to the right operand false

Less Than or Evaluates as true when the left operand is less 3 <= 5 returns
<=
Equal To than or equal to the right operand true
Exercise - Relational Operators

• Determine the result of the following conditional expressions,


given the following variables and their values:
int a = 250; int c = 350;
int b = 300; int d = 250;

1. a > b
2. b <= c
3. c != d
LOGICAL
OPERATORS
21
LOGICAL OPERATORS
• sometimes called a “Boolean operator”

• an operator that returns a Boolean result that’s based on


the Boolean result of one or two other expressions.

• Sometimes, expressions that use logical operators are


called “compound expressions” because the effect of the
logical operators is to let you combine two or more
condition tests into a single expression.
Logical Operators

Operator Name Type Description


! Not Unary Returns true if the operand to the
right evaluates to false. Returns false if
the operand to the right is true.
& And Binary Returns true if both of the operands
evaluate to true. Both operands are
evaluated before the And operator is
applied.
| Or Binary Returns true if at least one of the
operands evaluates to true. Both
operands are evaluated before the Or
operator is applied.
Logical Operators

Operator Name Type Description


&& Condition Binary Same as &, but if the operand
al And on the left returns false, it
returns false without evaluating
the operand on the right.
|| Condition Binary Same as |, but if the operand
al Or on the left returns true, it
returns true without evaluating
the operand on the right.

You might also like