You are on page 1of 21

OBJECTIVES:

1. To identify the different operators in an


expression
2. To differentiate the function of each
operator
3. To appreciate the importance of
operator/s in an expression
EXPRESSIONS

Expression in a programming language


is a combination of one or more constants,
variables, operators and functions that the
programming language interprets
(according to its particular rules of
precedence and of association) and
computes to produce another value.
OPERATORS

Operator in a programming language is a
symbol that tells the compiler or interpreter to
perform specific mathematical, relational or
logical operation and produce final result.
A compiler is a special program that
processes statements written in a
particular programming language and turns them
into machine language or "code" that a
computer's processor uses.
TYPES OF OPERATORS
 Arithmetic operators
 Relational operators

 Logical operators
 Assignment operators

 Increment and Decrement operators

 Conditional operators

 Bitwise operators
 Special operators
TYPES OF OPERATORS

 Arithmetic Operators

̶ These are the operators used to perform


arithmetic/mathematical operations on operands.

Priority Operator Meaning Associativity


1st () Parenthesis Left to right
2nd /, *, % Division, Multiplication, Left to right
Modulo

3rd +, - Addition, Subtraction Left to right


Example:
8/2 + 3*2 – 81% (75/3 – 10/2 + 9%3) + 25
8/2 + 3*2 – 81% (25 – 5 + 0)+ 25
8/2 + 3*2 – 81% (20)
+ 25
4 + 6 – 1 + 25
34
Example:
8/2 + 3*2 – 22% (75/3 – 1*10/2 + 9%2) + 26
8/2 + 3*2 – 22% (25 – 5 + 1)+ 26
8/2 + 3*2 – 22% (21)
+ 26
4 + 6 – 1 + 26
35
 Relational Operators
̶ In computer science, it is a programming
language construct or operator that tests or
defines some kind of relation between two
entities. It returns a value of True or False
Operator Priority Meaning/Name
> 1 Greater than
>= 1 Greater than or Equal to
< 1 Less than
<= 1 Less than or Equal to
== 2 Equal to
!= , <> 2 Not Equal to
Example:
Evaluate the given expressions below.

Expression Result
57 != 75 True

57 == 75 False

57 <= 75 True

57 >= 75 False

57 < 75 True
Logical Operators
̶ In computer science, is a symbol or word
used to connect two or more expressions such
that the value of the compound expression
produced depends only on that of the original
expressions and on the meaning of the operator.

Operator Name Explanation


&& AND Returns true if both x and y are true else returns
false
|| OR Returns false if neither x nor y is true else returns
true
! NOT Unary operator. Returns true if x is false else
returns false
TRUTH TABLES
Example:
Evaluate the given expressions below.

Expression Result
(6 > 4) AND (2 <= 14) 1st = t; 2nd = t; final = true
(6 < 4) AND (2 <= 14) 1st = f; 2nd = t; final = false
(6 > 4) OR (2 <= 14) 1st = t; 2nd = t; final = true
(6 < 4) OR (2 <= 14) 1st = f; 2nd = t; final = true
! (6 > 4) 1st = t; final = false
! ((10+2==(10))AND(13 != 7)) 1st = f; 2nd = t; 3rd = f; final = true
! ((8+2==(10))OR(13 != 7)) 1st = t; 2nd = t; 3rd = t; final = false
 Assignment Operators

̶ In programming, allows us to change the


value of a modifiable data object (variable).
It is associated with the concept of moving a
value into the storage location (variable). The
assignment operator has two operands. The one
to the left of the operator is usually an identifier
name for a variable. The one to the right of the
operator is a value.
Example 1:
Simple Assignment:

Age = 21

The value 21 is moved to the memory location for


the variable named: Age. Another way to say it:
Age is assigned the value 21.
Example 2:
Assignment with an Expression:

Total = 4 + 3 + 5 + 2

The item to the right of the assignment operator is


an expression. The expression will be evaluated
and the answer is 14. The value 14 would be
assigned to the variable named: Total
Example 3:
Assignment with Identifier Names in the
Expression:

Stud1 = 25
Stud_2 = 19
Total = Stud1 + Stud_2

The expression to the right of the assignment operator


contains some identifier names. The program would fetch
the values stored in those variables; add them together
and get a value of 44; then assign the 44 to the variable
Total
 Increment and Decrement Operators

 Increment Operator, increases the value of a


variable by 1.

 Decrement Operator, decreases the value of a


variable by 1.
++ and -- operator as prefix and postfix
 If you use the ++ operator as prefix like: +
+var. The value of var is incremented by 1
then, it returns the value.
 If you use the ++ operator as postfix like: var+
+. The original value of var is returned first
then, var is incremented by 1.
 The - - operator works in a similar way like the
++ operator except it decreases the value by 1.
References
https://study.com/academy/lesson/arithmetic-operators-in-pro
gramming-definition-examples.html
https://www.youtube.com/watch?v=RTc3bvIaHL8
https://press.rebus.community/programmingfundamentals/cha
pter/logical-operators/
https://www.hackerearth.com/practice/basic-programming/op
erators/basics-of-operators/tutorial/
https://press.rebus.community/programmingfundamentals/cha
pter/assignment/
https://www.programiz.com/article/increment-decrement-
operator-difference-prefix-postfix
Thank you!

You might also like