STE Computer Programming Q3 MODULE 2
STE Computer Programming Q3 MODULE 2
Computer
Programming
Quarter III – Module 2:
Apply different control statements in a program: If . . .
Else
"Designed by macrovector /
Freepik"
"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.
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
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
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
If you have any questions in using this SLM or any difficulty in answering the
Thank you.
ii
For the learner:
The hand is one of the most symbolized parts of the human body. It is often used to
depict skill, action, and purpose. Through our hands we may learn, create, and
accomplish. Hence, the hand in this learning resource signifies that you as a learner is
capable and empowered to successfully achieve the relevant competencies and skills at
your own pace and time. Your academic success lies in your own hands!
This module was designed to provide you with fun and meaningful opportunities for
guided and independent learning at your own pace and time. You will be enabled to
process the contents of the learning resource while being an active learner.
iii
you learned from the lesson.
this module.
1. Use the module with care. Do not put unnecessary mark/s on any part of the
3. Observe honesty and integrity in doing the tasks and checking your answers.
If you encounter any difficulty in answering the tasks in this module, do not hesitate
to consult your teacher or facilitator. Always bear in mind that you are not alone.
We hope that through this material, you will experience meaningful learning and
iv
v
Explore
Introduction:
A program is executed, then computer starts with the first statement and
moves towards the last statement, executing all the statements sequentially one
after the other. The program execution is like a waterfall. The waterfall, has water,
once it begins falling, will not stop until it touches the ground and then it would
flow, taking the downward slope. Similarly, once a computer begins a program
execution it will not stop until it reaches the last statement, which informs the
computer that the program has ended and instructs the computer to take all
necessary actions to stop the execution in a smooth manner and release all the
resources in the program. But, we cannot always allow the program to execute
like a waterfall. We need to take some programmed decisions or we need to take
control of our program, and let the program execution flow that depends on the
Control statements are the tools we use where we control the program
flow as we desire based on the programmed decisions we built into the program.
As the name implies, these statements control the flow of program execution.
Sub-Task:
PRE-ASSESSMENT
I. TRUE or FALSE. Write IF when the statement is correct, write ELSE when the
statement is an error. Write your answers on a separate paper.
program.
logical operators.
programming skills and mastering it is not essential to writing good quality computer
programs.
of 5 lines.
II. Fill in the bubbles. Write programming languages that uses “If . . . Else Statements.”
Write your answers in the bubble chart.
Arithmetic operators are the symbols that represent math operations in the
program. Relational and logical expressions is the case of whether the answer is
always either True or False. The operators are symbols that bring one, two or more
operands together to create an expression. The operands are the two key deciding
What is If statement?
if (X < 10) {
This statement is the most useful control statement for programmed decision making.
The If statement makes use of a relational or logical expression to make the decision. At
programming languages, the entire If ... Then ... Else ... statement is written on the same
line. But in most programming languages, the If statement spans across multiple lines.
https://cdn.programiz.com/sites/tutorial2program/files/cpp-if-else-working.png
In most programming languages, they use curly braces { } to enclose the statements
between Then ... Else and between Else ... They do not use an Endif statement at all, and
only begin and end the If statement with a set of curly braces { }.
An example using real life situation where we make a decision and we use multiple
expression and we need to use multiple statement to make the decision. Consider this
Let us use the variable labeled “age” to contain the value of the age of the person
FLOW DIAGRAM
START
Else
Endif
Else
Endif
END
We can use these conditions in performing different actions such as decision making.
We can use the following statements:
Use else to specify a block of code to be executed, if the same condition is false
Use else if to specify a new condition to test, if the first condition is false
Note: that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
1. An If statement needs to have at least (1) one relational expression that checks the
Example:
int main() {
int a = 5;
// !false = true
cout << !(a == 0) << endl;
Explanation:
In this program, we declare and initialize an int variable a with the value 5. We then
print a logical expression.
easily read in the computer screen. Recommended 2 lines for If . . . Else statements and
Example:
cout << "Good evening."; However, if the time was 14, our program
Q3_STE_Computer_Programming_ Module 2 Page 10 of 30
4. To facilitate easy analysis of the statement while debugging the program, it is
recommended to restrict logical expressions to 2. For every one logical expression needs
relational expressions. If the decision requires more than 2 logical expressions we can
nest them or use Boolean data type to assign the output of the evaluation of the
Example:
int main() {
int num;
// outer if condition
if (num != 0) {
// inner if condition
if ((num % 2) == 0) {
cout << "The number is even." << endl;
}
// inner else condition
Example explained:
Observe that using nested if...else makes your logic complicated. If possible,
you should always try to avoid nested if...else.
5. Writing if . . . else statement in multiple lines is highly recommended in most
understandable for debugging and maintenance purposes. You can also add comment
using //, to bookmark the part of the code that you will detect and give attention in the
program.
6. The possibility of ignoring the Else part of the If statement in some programs, is high.
However, we must always consider weird data can change the program and Else
*** Proficiency in using If . . . Else Statement is a skill to practice when writing a good
program.
Engage
INSTRUCTIONS:
1. In your laptop, desktop computer or cell phone, code this sample If . . . Else
Statement Syntax;
2. Then write your explanation in a separate paper. Five (5) points each.
Example:
Programming
CODE Explanation of the Output
Language
Apply
INSTRUCTIONS:
1. Write a simple program and apply If . . . Else statement to determine your
grade in Computer Programming.
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
I. TRUE or FALSE. Write IF when the statement is correct, write ELSE when the
statement is an error. Write your answers on a separate paper.
I. TRUE or FALSE. Write IF when the statement is correct, write ELSE when the
statement is an error. Write your answers on a separate paper.
program.
logical operators.
programming skills and mastering it is not essential to writing good quality computer
programs.
of 5 lines.
II. Fill in the bubbles. Write programming languages that uses “If . . . Else Statements.”
Write your answers in the bubble chart.
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_______________________.
_________________________________________________________________
_________________________________________________________________