You are on page 1of 22

10

Computer
Programming
Quarter III – Module 1:
Arithmetic, Logical and Relational Operators

"Designed by macrovector /
Freepik"
Computer Programming – Grade 10
Self-Learning Module
First Edition, 2020

Republic Act 8293, section 176 states that: No copyright shall subsist in any work
of the Government of the Philippines. However, prior approval of the government agency or
office wherein the work is created shall be necessary for exploitation of such work for profit.
Such agency or office may, among other things, impose as a condition the payment of
royalties.

Borrowed materials (i.e., songs, stories, poems, pictures, photos, brand names,
trademarks, etc.) included in this module are owned by their respective copyright holders.
Every effort has been exerted to locate and seek permission to use these materials from
their respective copyright owners. The publisher and authors do not represent nor claim
ownership over them.

Published by the Department of Education – Regional Office VIII


Regional Director: Ma. Gemma Mercado Ledesma
Assistant Regional Director: Bebiano Inhog Sentillas

Printed in the Philippines by ________________________

Department of Education – Regional Office VIII

Office Address: Government Center, Candahug, Palo, Leyte

Telefax: 053 - 3233156


E-mail Address: region8@deped.gov.
Introductory Message
This Self-Learning Module (SLM) is prepared so that you, our dear learners, can

continue your studies and learn while at home. Activities, questions, directions,

exercises, and discussions are carefully stated for you to understand each lesson.

Each SLM is composed of different parts. Each part shall guide you step-by-

step as you discover and understand the lesson prepared for you.

At the end of each module, you need to answer the test to self-check your

learning. Answer keys are provided for each activity and test. We trust that you will be

honest in using these.

In addition to the material in the main text, Notes to the Teacher are also

provided to our facilitators and parents for strategies and reminders on how they can

best help you on your home-based learning.

Please use this module with care. Do not put unnecessary marks on any part of

this SLM. Use a separate sheet of paper in answering the exercises and tests. And

read the instructions carefully before performing each task.

If you have any questions in using this SLM or any difficulty in answering the

tasks in this module, do not hesitate to consult your teacher or facilitator.

Thank you.
Explore

Most Essential Learning Competency

Illustrate the use of Arithmetic, Logical and Relational Operators.

After going through this self-learning module, you are expected to:

1. Identify the different Arithmetic, Logical and Relational Operators.

2. Define the different Arithmetic, Logical and Relational Operators.

3. Apply Arithmetic, Logical and Relational Operators on a computer

program.

In programming, an operator is a special symbol that tells the computer

to perform mathematical, relational or logical operation to produce a desired

output. There are many types of operators in the computer programming world,

some can only be found on specific programming language, but these three is

common to all.

The figure below illustrates the role of an operator in a computer program.

It needs an input value such as numbers and letters to function properly. The

operator works like the brain of a human being that can tell the difference

between two objects, intelligent enough to make decision based on the values

of variables that are fed into it.

INPUT OPERATOR OUTPUT


This module will explain the concept of operators and it will take you

through the important arithmetic, relational and logical operators available in C,

Javascript, Java, and Visual Basic.

Learn

In your previous lesson, we learned how to design a flowchart of a simple


program.

A flowchart is a visual outline of an algorithm in which the steps and

processes to be followed are represented by symbols. There are actually two kinds

of flowcharts: the system flowcharts and the program flowchart

A system flowchart is a diagrammatic representation of the broad flow of

work, documents and operations.

A program flowchart, on the other hand, is a diagrammatic representation of

a solution to a given problem for which the program is written.

Now, we will learn what are the different types of operators in computer

Drill: Wolf, Goat, Cabbage problem

Once upon a time a farmer went to a market and purchased a wolf,


a goat, and a cabbage. On his way home, the farmer came to the bank of a river
and rented a boat. But crossing the river by boat, the farmer could carry only
himself and a single one of his purchases: the wolf, the goat, or the cabbage.
If left unattended together, the wolf would eat the goat, or the goat would
eat the cabbage.

The farmer's challenge was to carry himself and his purchases to the far
bank of the river, leaving each purchase intact. How did he do it?

Where you able to solve the problem of the farmer? Do you know that you can

make a simple program that can help the farmer?

Here is a program that the farmer can use to check which of his purchased item can be

left together on either side of the river.

Here’s a program flowchart that you can follow when you start coding later.
Item Value Legend:
start Cabbage = 1
Goat = 2
Wolf = 3
Which items will you
leave behind?

Enter Item1
Enter Item2

F F
Item1 > Item2 (Item2 -1)
“Your Items
= Item1
are safe”

T
T
F
(Item1 -1) = “Your Items “Item2 will eat
Item2 are safe” Item1”

“Item1 will eat


Item2”

stop
Arithmetic Operators

Computer programs are widely used for mathematical calculations. We

can write a computer program which can do simple calculation like adding two

numbers (2 + 3) and we can also write a program, which can solve a complex

equation like P(x) = x4 + 7x3 - 5x + 9. In the expression 2+3, we are aware that

the numbers 2 and 3 are operands, and the + sign is an operator. Similar

concepts exist in Computer Programming.

Take a look at the following two examples

1. 2 + 3
4 3
2. P(x) = x + 7x - 5x + 9.

These two statements are called arithmetic expressions in a programming

language. The plus and minus signs used in these expressions are called

arithmetic operators and the values used in these expressions like 2, 3, x, etc.,

are called operands. In their simplest form, such expressions produce numerical

results.

Similarly, a programming language provides various arithmetic operators. The

following table lists down a few of the important arithmetic operators available

in C programming language. Assume variable A holds 10 and variable B holds 20,

then

Operators Description Example

+ Adds two operands A+B will give 30

- Subtracts second operand from the first A–B will give -10
* Multiplies both operands A*B will give 200

/ Divides numerator by de-numerator B/A will give 2

% This gives remainder of an integer division B%A will give 0

The following is a simple example of C Programming to understand the

above mathematical operators:

#include <stdio.h>

int main() {
int a, b, c;

a = 10;
b = 20;

c = a + b;
printf( "Value of c = %d\n", c);

c = a - b;
printf( "Value of c = %d\n", c);

c = a * b;
printf( "Value of c = %d\n", c);

c = b / a;
printf( "Value of c = %d\n", c);

c = b % a;
printf( "Value of c = %d\n", c);
}

When the code program is executed, it produces the following results

Value of c = 30
Value of c = -10
Value of c = 200
Value of c = 2
Value of c = 0
Simple example in Javascript Programming

var x = 5;
var y = 2;
var z = x - y;
console.log(z);

Simple example in Java Programming


class Main {

public static void main(String[] args) {

// declare variables

int a = 12, b = 5;

// addition operator

System.out.println("a + b = " + (a + b));

// subtraction operator

System.out.println("a - b = " + (a - b));

// multiplication operator

System.out.println("a * b = " + (a * b));

// division operator

System.out.println("a / b = " + (a / b));

Simple example in Visual Basic Programming

Dim x As Integer
x = 67 + 34
x = 32 - 12
Logical Operators

Logical operators are very important in any programming language, and


they help us take decisions based on certain conditions. Suppose we want to
combine the result of two conditions, then logical AND, OR and NOT logical
operators help us in producing the final result.

Logical operators are mainly used to control program flow. Usually, you
will find them as part of an if, while, or some other control statement

The following table shows all the logical operators supported by the C language.

Assume variable A holds 1 and variable B holds 0, then

Operators Description Example

&& Called Logical AND operator. If both the (A && B) is false

operands are non-zero, then condition becomes

true.

|| Called Logical OR Operator. If any of the two (A || B) is true.

operands is non-zero, then condition becomes

true.

! Called Logical NOT Operator. Use to reverses !(A && B) is true.

the logical state of its operand. If a condition is

true then Logical NOT operator will make false.

Try the following example to understand all the logical operators available

in C programming language
#include <stdio.h>

int main() {
int a = 1;
int b = 0;

if ( a && b ) {

printf("This will never print because condition is false\n" );


}
if ( a || b ) {

printf("This will be printed print because condition is


true\n" );
}
if ( !(a && b) ) {

printf("This will be printed print because condition is


true\n" );
}
}

When you compile and execute the above program, it produces the following results:

This will be printed print because condition is true


This will be printed print because condition is true

Simple example in Javascript Programming


let x = 1;
if (x > 0) alert( 'Greater than zero!' );
let hour = 9;
if (hour < 10 || hour > 18) {
alert( 'The office is closed.' );
}
Simple example in Java Programming

class Main {
public static void main(String[] args) {
// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false
// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false
// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}

Simple example in Visual Basic Programming

Dim name As String = "Kenjie"


Dim age As Integer = 12

If name = "Kenjie" And age = 12 Then


'the statements return True
End If

If name = "Kenjie" Or age = 13 Then


'the statements return True
End If

If name = "Kenjie" And age = 13 Then


'the statements return False
End If

Relational Operators

Relational operators are important for making decisions. They allow us to


compare numeric and char (chars are treated like numbers in C++) values to
determine if one is greater than, less than, equal to, or not equal to another.

Relational operators are binary, meaning they require two operands.


Relational operators have left to right associativity. Left to right
associativity means that when two operators of same precedence are adjacent,
the left most operator is evaluated first.

Consider a situation where we create two variables and assign them


some values as follows

A = 20
B = 10

Here, it is obvious that variable A is greater than B in value. So, we need


the help of some symbols to write such expressions which are called relational
expressions. If we use C programming language, then it will be written as
follows

(A > B)

Here, we used a symbol > and it is called a relational operator and in


their simplest form, they produce Boolean results which mean the result will be
either true or false. Similarly, a programming language provides various
relational operators. The following table lists down a few of the important
relational operators available in C programming language. Assume
variable A holds 10 and variable B holds 20,

Relational Meaning Example

Operators

> Checks if the value of left operand is greater (A > B) is not true

than the value of right operand, if yes then

condition becomes true.


< Checks if the value of left operand is less than (A < B) is true.

the value of right operand, if yes then condition

becomes true.

>= Checks if the value of left operand is greater (A >= B) is not true.

than or equal to the value of right operand, if

yes then condition becomes true.

<= Checks if the value of left operand is less than (A <= B) is true.

or equal to the value of right operand, if yes

then condition becomes true.

== Checks if the values of two operands are equal (A == B) is not true.

or not, if yes then condition becomes true.

!= (A != B) is true
Checks if the values of two operands are equal
or not, if values are not equal then condition
becomes true.

Here, is one example of C Programming which makes use of if

conditional statement. This statement will be discussed later in a separate

module, but to give you an idea, we use if statement to check a condition and if

the condition is true, then the body of if statement is executed, otherwise the

body of if statement is skipped


#include <stdio.h>

int main() {
int a, b;
a = 10;
b = 20;

/* Here we check whether a is equal to 10 or not */


if( a == 10 ) {

/* if a is equal to 10 then this body will be executed */


printf( "a is equal to 10\n");
}

/* Here we check whether b is equal to 10 or not */


if( b == 10 ) {

/* if b is equal to 10 then this body will be executed */


printf( "b is equal to 10\n");
}

/* Here we check if a is less b than or not */


if( a < b ) {
When the above program is executed, it produces the following results:
/* if a is less than b then this body will be executed */
printf( "a is less than b\n");
a }is equal to 10
a is less than b
a /*
is Here
not we check
equal towhether
b a and b are not equal */
if( a != b ) {

/* if a is not equal to b then this body will be executed */


printf( "a is not equal to b\n");
}
}

Simple example in Javascript Programming

function myFunction() {

var age, voteable;

age = document.getElementById("age").value;

voteable = (age < 18) ? "Too young":"Old enough";

document.getElementById("demo").innerHTML = voteable + " to vote.";

Simple example in Java Programming


class Main {

public static void main(String[] args) {

// create variables
int a = 7, b = 11;

// value of a and b
System.out.println("a is " + a + " and b is " + b);

// == operator
System.out.println(a == b); // false

// != operator
System.out.println(a != b); // true

// > operator
System.out.println(a > b); // false

// < operator
System.out.println(a < b); // true

// >= operator
System.out.println(a >= b); // false

// <= operator
System.out.println(a <= b); // true
}
}

Simple example in Visual Basic Programming

Dim x As Integer = 5, y As Integer = 20


Dim result As String
'if...else statement
If x > y Then result = "x greater than y"
Else
result = "x less than y"
End If

Engage

Instruction: Match Column A with the correct statements of operators in


Column B by drawing a line to connect them.
COLUMN A COLUMN B

1. Two integers and check A. If (Input1 >= 0) then


whether they are equal
or not

2. Check if an hour is B. Value = 2021 – Input1


within the store
operating hours of 8am
to 5pm.

3. Compute the age of the C. If (Input1 == Input2) then


person.
4. Check whether a given D. If (Input1 >= 18) then
number is positive or
negative
5. Check the age of a E. If (Input1 >= 8 && Input1 <= 17) then
candidate and
determine whether
he/she qualified to
vote or not.
Apply

INSTRUCTIONS:
1. Write a program using any of the operators in our lesson, to accept 3 non-
equal numbers and will automatically identify the largest value of the 3.
2. You can submit your code via my email address or write it on the space provided.

CRITERIA 1 5 6 8 10
ACCURACY The output is The output has The output has The output has The output has
totally incorrect. shown 4 – 5 shown 2 – 3 shown 1 error. no error/s.
errors. errors.
CODE EFFICIENCY Codes do not Codes generate Codes generate Codes generate Codes generate
work. output with 4 – output with 2 – output with 1 the output
5 errors. 3 errors. error. desired.
TIMELINESS Did not submit Exceeded a Submitted 3 – Submitted 1 – Submitted on
at all. week or more 5 days after the 2 days after or before the
after deadline. deadline. deadline. deadline.

TOTAL
4. The activity will be scored using the Rubrics below:

WRITE

Assess

Multiple Choice. Choose the letter of the best answer. Write the chosen letter
on a separate sheet of paper.

1. It is called Logical OR Operator. If any of the two operands is non-zero,


then condition becomes true.
a. &&
b. ||
c. >=
d. ==

2. It checks if the value of left operand is greater than or equal to the value
of right operand, if yes then condition becomes true.
a. <
b. >
c. <=
d. >=

3. They allow us compare numeric and char values to determine if one is


greater than, less than, equal to, or not equal to another.

a. Arithmetic
b. Logical
c. Relational
d. Operator

4. It checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true.
a. <
b. >
c. >=
d. <=

5. It is called Logical AND operator. If both the operands are non-zero, then
condition becomes true.
a. &&
b. ||
c. >=
d. !
Reflect

Complete the following statements:

Today, I discovered that ___________________________________

________________________________________________________________

________________________________________________________________

________________________________________________________________

________________________________________________________________

________________________________________________________________

____________________________.

I encountered difficulties in________________________________

________________________________________________________________

________________________________________________________________

You might also like