0% found this document useful (0 votes)
102 views15 pages

If...Else Statement in Programming

This document is a self-learning module for Grade 10 Computer Programming, focusing on the application of control statements, specifically the If...Else statement. It includes instructions, activities, and assessments designed to guide learners through understanding and implementing these programming concepts. The module emphasizes the importance of decision-making in programming and provides examples and exercises for practice.

Uploaded by

saksakpare
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views15 pages

If...Else Statement in Programming

This document is a self-learning module for Grade 10 Computer Programming, focusing on the application of control statements, specifically the If...Else statement. It includes instructions, activities, and assessments designed to guide learners through understanding and implementing these programming concepts. The module emphasizes the importance of decision-making in programming and provides examples and exercises for practice.

Uploaded by

saksakpare
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

10

Computer
Programming
Quarter III – Module 2:
Apply different control statements in a program: If . . .
Else

"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: Ramir B. Uytico EdD, CESO IV
Assistant Regional Director: Arnulfo M. Balane, CESO V

Development Team of the Module


Writers: Ma. April E. Estigoy
Language Editors: John Darrel Balanquit
Content Editors: Maricar C. Yturriaga, Eufemio D. Adarayan Jr.
Illustrators: James Mark D. Acebuche
Layout Artist:
Management Team:
Rosemarie M. Guino EdD, OIC – Chief, CLMD
Ryan R. Tiu EdD, EPS, CLMD – Science
Joy B. Bihag, EPS, CLMD – LRMS
Alex Rejuso, Ph.D, Chief, CID
Emily Adrayan, Ed.D, EPS, CID – Science
Isagani Licas, EPS, CID - LRMS

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@[Link].
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.

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.

This module has the following parts and corresponding icons:

This will give you an idea of the skills or


Explore
competencies you are expected to learn in the
module. A brief drill or review to help you link
the current lesson with the previous one. The
new lesson will also be introduced to you in
various ways such as a story, a song, a poem,
a problem opener, an activity, or a situation.
This section provides a brief discussion of the
Learn
lesson. This aims to help you discover and
understand new concepts and skills.

Engage This comprises activities for independent


practice to solidify your understanding and
skills of the topic. You may check the answers
to the exercises using the Answer Key at the
end of the module.
This includes questions or blank
Apply sentence/paragraph to be filled into process
what you learned from the lesson.

Assess This is a task which aims to evaluate your


level of mastery in achieving the learning
competency.
This contains answers to all activities in the
Answer Key module.

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.

At the end of this module you will also find:


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
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 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.

There are different types of control statements, in this module we will


discuss “If. . . . Else” statements.

After going through this module, you are expected to:

Apply the control statement – If . . . Else in a program.

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.

Q3_STE_Computer_Programming_ Module 2 Page 1 of 17


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.

______________1. If statement uses Boolean Expression data type.

______________2. All programming languages uses If . . . Else Statements to control

a program.

______________3. It is impossible to ignore the Else part of the If statement in a

program.

______________4. An If statement can have multiple relational expressions joined by

logical operators.

______________5. If . . . Else statement uses curly bracket ({ })to enclose

statements.

______________6. Writing If . . . Else statement in multiple lines is recommended for

debugging & maintenance purposes.

______________7. The If statement is perhaps the most important of the computer

programming skills and mastering it is not essential to writing good quality

computer programs.

______________8. In real life, we also apply if . . . else statements in making a

decision.

______________9. When nesting an If statement, it is recommended to limit it to

maximum of 5 lines.

______________10. While there is restriction on the number of expressions in an If

statement, it is suggested to restrict the number of logical expressions to 2.

Q3_STE_Computer_Programming_ Module 2 Page 2 of 17


II. Fill in the bubbles. Write programming languages that uses “If . . . Else
Statements.” Write your answers in the bubble chart.

______________________________

_______________________________ _______________________________

IF... Else
Statement
s

______________________________ ______________________________

Q3_STE_Computer_Programming_ Module 2 Page 3 of 17


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 more 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 conditional statements . . . . If . . . Else Statement.

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) {

print "Hello John";

What is Else statement?


Else is a programming conditional/ decision making statement that if previous
conditions are not true displays alternate information or performs alternate
commands.

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".

Q3_STE_Computer_Programming_ Module 2 Page 4 of 17


What is If . . . Else statement?
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 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 same line. But in most programming languages, the If statement
spans across multiple lines.

It generally takes the form:

If <relational or logical expression> Then


Statement
Statement
...
...
Else
Statement
Statement
...
...
Endif

If . . . Else statement evaluate the condition inside the parenthesis.

[Link]

Q3_STE_Computer_Programming_ Module 2 Page 5 of 17


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 classification of people by their age:

1. If the person is between 5 and 12 years old, that person is a child

2. If the person is between 20 and 65 years old, that person is an adult

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.

If the (age >= 5) and (age <= 65) Then

If the (age <= 12) Then

Specify the person as a child

Else

Specify the person as an adult

Endif

Else

Specify the person as not categorized

Endif

Q3_STE_Computer_Programming_ Module 2 END Page 6 of 17


We can use these conditions in performing different actions such as decision
making. We can use the following statements:

 Use if to specify a block of code to be executed, if a specified condition is


true
 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

If . . . Else statement supports logical conditions from mathematics:

 Less than: a < b


 Less than or equal to: a <= b
 Greater than: a > b
 Greater than or equal to: a >= b
 Equal to a == b
 Not Equal to: a != b

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

“C++ If . . .Else,” Accessed January 4, 2021,


[Link]

“Conditional Statement,” Computer Hope, September 15, 2017,


[Link]

“C++ Flow Control – C++ if . . . else,” Accessed January 4, 2021,


[Link]

Q3_STE_Computer_Programming_ Module 2 Page 7 of 17


Q3_STE_Computer_Programming_ Module 2 Page 8 of 17
For inquiries or feedback, please write or call:

Department of Education – Regional Office VIII – Curriculum and Learning


Management Division (CLMD) - Learning Resources Management Section (LRMS)

Government Center, Candahug, Palo, Leyte, 6501

Telefax: (053) 323-3156; 323-3854; 824-4627

Email Address: *region8@[Link]


*clmd.region8@[Link] *lrmds.region8@[Link]

Common questions

Powered by AI

Control statements in programming, such as the If...Else statements, mirror real-life decision-making scenarios by allowing for the execution of different actions based on certain conditions being met. In real-life, for example, one might use similar logic when making decisions about categorizing age groups: if a person's age is between 5 and 12, they are classified as a child, whereas if between 20 and 65, they are categorized as an adult . Similarly, in programming, control statements enable a program to perform specific actions depending on the outcomes of relational or logical expressions, reflecting how conditions influence decisions in everyday life .

The syntactical requirements for 'If...Else' statements can vary significantly between programming languages. Typically, most languages use curly braces {} to enclose the statements between Then...Else, eliminating the need for an explicit 'Endif' statement. This is distinct from some languages that allow the entire 'If...Then...Else' to be written in a single line . Furthermore, languages like C++ require 'if' to be in lowercase, as variations, like 'If' or 'IF', would generate syntax errors. Such rules emphasize how syntactic conventions can impact program correctness in different programming languages .

Relational and logical expressions are crucial for decision-making in programming as they form the conditions that drive control structures like If...Else statements. Relational expressions evaluate comparisons between operands, such as whether a value is less than or equal to another, while logical expressions can combine multiple relational expressions using operators like AND, OR. The outcome of these expressions determines which block of code is executed, making them essential for guiding program flow based on certain criteria .

Honesty and integrity are crucial in the completion of tasks within the self-learning module as they promote sincere learning and accurate self-assessment. The module emphasizes these virtues for learners to genuinely understand their abilities and master the content without relying on unchecked answers. This ethics-focused approach intends to foster responsible learning practices and credibility in educational achievements .

Optimal practices for writing 'If...Else' statements include writing them across multiple lines, limiting the nesting of If statements to a maximum of 5 lines, and restricting the number of logical expressions to 2. These practices are recommended to enhance readability, simplify debugging and maintenance, and reduce complexity, which can lead to fewer errors in program execution and clearer program structure .

Mastering the 'If' statement is considered vital in programming because it is foundational to decision-making processes in coding. Despite its simplicity, it allows for the condition-based execution of code blocks, facilitating complex decision-making and enabling programmers to add flexibility and intelligence to their programs. Without mastering 'If' statements, writing good quality programs that can handle a variety of scenarios and inputs becomes challenging, thus making them indispensable in programming skills .

The self-learning module guides learners through the learning process of control statements by providing structured sections: 'Explore' introduces the skills and competencies expected to learn, 'Learn' offers brief discussions on new concepts, 'Engage' includes activities for practice, 'Apply' supports processing of the lesson, 'Assess' evaluates mastery, and 'Reflect' encourages learners to analyze their understanding. Each section builds upon the previous, allowing a sequential approach to mastering content, highlighting the module's thoughtfully designed framework for independent learning .

The module ensures learners are active participants by incorporating sections like 'Engage' for independent practice, 'Assess' for evaluating comprehension, and 'Reflect' for personal reflection on strengths and weaknesses in understanding the material. These activities require learners to actively engage with the content, apply knowledge, and critically assess their learning journey, thus fostering an interactive learning environment that encourages continuous engagement .

Yes, the 'If...Else' statement can be applied to processes outside programming. For instance, in customer service scenarios, a company may have a decision tree: If a customer's complaint is about a recent transaction and the transaction is confirmed, then process a refund. Else, investigate further to verify the details. This mimics an 'If...Else' logic where the company's actions depend on reviewing the condition of transaction confirmation, demonstrating how programming concepts can streamline decision-making in real-world operations .

If learners encounter difficulties, the module recommends consulting their teacher or facilitator. This approach underlines the importance of seeking help to overcome challenges, thereby maintaining a supportive learning atmosphere that encourages dialogue and ensures learners do not struggle alone. This strategy aligns with the module's aim to assist learners in gaining a deep understanding without feeling isolated in their learning process .

You might also like