Module 2 Ste Computer Programming if Else Statement
Module 2 Ste Computer Programming if Else Statement
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
For the learner:
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
This contains the learner’s reflection. Learners
Reflect
are encouraged to think about the lessons
particularly the parts that went well (they
have understood) and the parts that were
weak (they have difficulty) and write about it
briefly. Learners can share their thoughts and
feeling about the lessons.
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
v
Explore
Introduction:
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.
a program.
program.
logical operators.
statements.
computer programs.
decision.
maximum of 5 lines.
______________________________
_______________________________ _______________________________
IF... Else
Statement
s
______________________________ ______________________________
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) {
print "X is equal to 10"; The Code sets the
x variable first as 10. If this
} value remains 10, then the
first if statement prints "X is
else {
equal to 10". But, if this value
print "X is not equal to 10"; changes it would print "X is
} not equal to 10".
https://cdn.programiz.com/sites/tutorial2program/files/cpp-if-else-working.png
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 classification of people by their age:
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.
References
Murali Chemuturi, Computer Programming for Beginners: A step by step Guide
(New York: CRC Press, 2019), 74 - 76