STE - Computer Programming Q3 MODULE 2 - 094143
STE - Computer Programming Q3 MODULE 2 - 094143
Computer
Programming
Quarter III – Module 2:
Apply different control statements in a
program: If . . . Else
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.
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.
ii
Welcome to the Computer Programming – Learning Module 2 on Apply different
control statements in a program: If . . .Else Statement.
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
References This is a list of all sources used in developing
this module.
The following are some reminders in using this module:
1. Use the module with care. Do not put unnecessary mark/s on any part of the
module. Use a separate sheet of paper in answering the exercises.
2. Read the instructions carefully before doing each task.
3. Observe honesty and integrity in doing the tasks and checking your answers.
4. Finish the task at hand before proceeding to the next.
5. Return this module to your teacher/facilitator once you are finished.
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
gain deep understanding of the relevant competencies. You can do it!
iv
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 outcome of a programmed decision.
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:
a. Understand If . . . Else statements;
b. Discuss the general rules for writing If . . . Else statement;
c. Write a simple program that applies If . . . Else statements.
PRE-ASSESSMENT
Q3_STE_Computer_Programming_ Module 2 Page 1 of 23
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.
program.
logical operators.
programming skills and mastering it is not essential to writing good quality computer
programs.
maximum of 5 lines.
II. Fill in the bubbles. Write programming languages that uses “If . . . Else
Statements.” Write your answers in the bubble chart.
_______________________________ _______________________________
IF... Else
Statements
______________________________ ______________________________
Learn
In your previous lesson, we studied about arithmetic, logical and relational
expression.
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 3more
Page of 23
Q3_STE_Computer_Programming_ Module 2
operands together to create an expression. The operands are the two key deciding
factors of the outputs.
Now, we will study how to use these expressions in decision-making and/or
What is If statement?
If statement is a programming conditional/ decision making statement that, if proved
true, performs a function or displays information. Below is a general example of an if
statement, not specific to any particular programming language.
if (X < 10) {
my $x = 10; Explanation:
if ($x == 10) { The Code sets the
print "X is equal to 10"; x variable first as 10. If this
} value remains 10, then the first
if statement prints "X is equal
else { to 10". But, if this value
print "X is not equal to 10"; changes it would print "X is not
} equal to 10".
The If statement makes use of a relational or logical expression to make the decision.
At a minimum, one relational expression is essential for an If statement. In some
programming languages, the entire If ... Then ... Else ... statement is written on the
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 { }.
Then, we use these rules for selecting a person’s life development. We need to develop
a set of control statements to make the right decision.
Let us use the variable labeled “age” to contain the value of the age of the person
between 1 and 100. With this let us form a set of:
FLOW DIAGRAM
START
If statements to arrive at the right decision.
Else
Endif
Else
Endif
Note: that if is in lowercase letters. Uppercase letters (If or IF) will generate an
error.
Example:
Example:
// C++ program demonstrating ! logical operator truth
table
#include <iostream>
using namespace std;
int main() {
int a = 5;
// !false = true
cout << !(a == 0) << endl;
Q3_STE_Computer_Programming_ Module 2 Page 7 of 23
// !true = false
cout << !(a == 5) << endl;
return 0;
3. There is no limit in using If statement, however it is recommended that lines can be
easily read in the computer screen. Recommended 2 lines for If . . . Else statements
and the use of assignment statement using data types.
Example:
Example:
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
statement may be needed. It is recommended to embed Else statement even if there is
no variable between the Else and Endif keywords.
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
Programming
CODE Explanation of the Output
Language
System.out.println("Good
day.");
} else {
System.out.println("Good
evening.");
}
Console.WriteLine("Good
day.");
}
else
{
Console.WriteLine("Good
evening.");
}
Apply
TOTAL
CODE HERE:
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.
program.
logical operators.
programming skills and mastering it is not essential to writing good quality computer
programs.
maximum of 5 lines.
II. Fill in the bubbles. Write programming languages that uses “If . . . Else
Statements.” Write your answers in the bubble chart.
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_______________________.
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
___________________________.
Answer Key