COMPUTER SCIENCE
Quarter 4 – Module 1:
Operators in C++
Self-Learning Module
Learner’s Material
I Need to Know
In this module, you will be introduced to different learning activities preparing you
to be skillful before you take the plunge into the world of Programming.
This module is specifically crafted to focus on Programming Language C++
enriched with learning activities that will assess your level in terms of skills and knowledge.
After going through this module, you are expected to
1. define an operator, relational operators and logical operators,
2. identify the different types of operators and when to use it in
programming, and,
3. write statements as relational and logical expressions.
I Will Check On This
True or False. Write T if the statement is correct and F if it is otherwise.
1. Increment Operator in C++ decreases the value of a variable by 1.
2. The ! Operator is used to change an expression to its opposite state.
3. A Relational operator is a programming language construct that tests or defines
some kind of relation between two entities.
4. Float data type is used for whole numbers.
5. The double pipe symbol || identifies the programmer and the purpose of the
program.
6. The operator used in C++ to send output to the computer screen is >>.
7. An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulations.
8. Logical operators are sometimes called Boolean operators, that evaluate
expressions and decide what Boolean should be expressed from the evaluation.
9. If both the operands are non-zero, then condition becomes true, hence, the
Logical OR operator is applied.
10. All of the relational operators result in a Boolean value (TRUE or FALSE).
2
Lesson
1 Operators in C++
Lesson Overview
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 first expression, 2 and 3 are operands and + is an operator. Similar concepts
exist in Computer Programming.
An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulations. C++ is rich in built-in operators and provides the following types of
operators:
Arithmetic/Mathematical Operators
Relational Operators
Logical Operators
Arithmetic Operators in C++
Assume variable A holds 10
++ Increment operator - increases integer value by one
A++ will give 11
-- Decrement operator - decreases integer value by one
A-- will give 9
Relational Operators
A relational operator is a programming language construct or operator that tests
or defines some kind of relation between two entities. These include numerical equality
(e.g., 5 = 5) and inequalities (e.g., 4 > 3). All of the relational operators result in a Boolean
value (TRUE or FALSE).
There are six relational operators used for data comparisons in C++. These
operators must always appear between 2 literals, 2 variables, 2 arithmetic expressions, or
a combination of these possibilities.
3
Assume variable A holds 10 and variable B holds 20
Logical Operators
Logical operators, sometimes called boolean operators, evaluate expressions and
decide what boolean should be expressed from the evaluation.
Assume variable A holds 1 and variable B holds 0
4
I Will Do This
Let’s Review. This is a Program Completion, just fill in the missing item to complete
the program.
//Miguelito Santos
//A program that computes the sum of three whole numbers.
#include 1. ________
using namespace std;
int main( )
2. ________
3. ________ num1, num2, 4. ________, 5. ________; //variable declaration
cout <<”Enter first number:”;
cin >> 6. ________;
cout <<”Enter second number:”;
cin >> 7. ________;
cout <<”Enter third number:”;
cin >> num3;
sum= num1 + 8. ________ + num3;
cout << “The sum of three numbers:” << 9. ________;
return 0;
10.________;
I Learned This
I have learned that C++ Programming Language is rich in built-in operators. Aside
from Arithmetic Operators, C++ supports Relational and Logical Operators. Relational
Operators are
______________________________________________________________________
______________________________________________________________________
_______________________________________________, while Logical Operators are
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________.
5
I Practice This
For each of the following statements, assign variable names for the unknowns and
rewrite the statements as relational expressions. Remember in writing relational
expressions, an operator is always in between a variable and a literal value.
Example: Voting age is 18 and above – A >= 18
A is the assigned variable
>= greater than or equal to is used as an operator
18 is the given value
1. A customer's age is 65 or more.
2. The temperature is less than 0 degrees.
3. A person's height is over 6 feet.
4. The current month is 12 (December).
For each of the following statements, assign variable names for the unknowns and
rewrite the statements as logical expressions.
Example: The current day is the 7th of the 4th month - D==7 && M==4
D and M are the assigned variables
== equal to and && logical and are used as relational and logical operators
7 and 4 are the given value for day and month respectively
1. The person's age is 65 or more and their sub total is more than $100.
2. A person is older than 55 or has been at the company for more than 25 years.
3. A width of a wall is less than 4 metres but more than 3 metres.
4. An employee's department number is less than 500 but greater than 1, and
they've been at the company for more than 25 years.
I Apply This
The following table illustrates using an operator’s precedence and associativity to
evaluate relational expressions, assuming the following declarations:
int i = 5, j = 7, k = 12;
6
Complete the table by writing its value (0 or 1) and its interpretation (True or False).
Expression Value Interpretation
i + 2 == k - 1
3 * i - j < 22
i+2*j>k
k + 3 < -j + 3 * i
j * i != k * 3 - 1
Post Test
Read each sentence carefully. Select the letter of the best answer.
1. Which of the following data type is used for whole numbers?
A. Float
B. Int
C. Char
D. Double
2. It is a symbol that tells the compiler to perform specific mathematical or logical
manipulations.
A. Comments
B. Header file
C. Operator
D. Compiler
3. Which of the following operator increases an integer value by one?
A. Increment Operator
B. Decrement Operator
C. Relational Operator
D. Logical Operator
4. Operators must always appear between one of the following
A. 2 literals
B. 2 variables
C. 2 arithmetic expressions
D. All of the above
5. Which of the following relational operator is used to check if the value of left
operand is less than or equal to the value of right operand?
A. <=
B. >=
C. ++
D. ==
7
6. What is the other term for Logical Operators?
A. Arithmetic Operators
B. Boolean Operators
C. Increment Operators
D. Binary Operators
7. If both the operands are non-zero in a logical expression, then one of the following
operator is applied
A. Logical OR
B. Logical NOT
C. Logical AND
D. None of the above
8. How do you write the statement, Voting age is 18 and above into relational
expression?
A. Age > 18
B. Age <= 18
C. Age == 18
D. Age >=18
9. Which of the following statement is TRUE about the Logical Operator OR?
A. If both the operands are non-zero
B. If the values of two operands are equal or not
C. If any of the two operands is non-zero
D. None of the above
10. This operator is used to reverses the logical state of its operand. If a condition is
true then this operator will make it false.
A. !
B. &&
C. ||
D. ++
REFERENCES
Introduction to Turbo C Programming by Copernicus P. Pepito Pages 6-9
Basic C++ for Engineers and Scientists by Gary J. Bronson
Anvil Publishing Inc. Page 45
https://www.programiz.com/cpp-programming/relational-logical-operators
Mathbitz.com
https://www.tutorialspoint.com/computer_programming/computer_programming_operators
For inquiries and clarifications:
Hannah Marie Jolo-Dojoles
09173003745 / 09479469720