0% found this document useful (0 votes)
764 views

STE Computer Programming Q3 MODULE 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
764 views

STE Computer Programming Q3 MODULE 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

10

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.

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: [email protected].
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:

Explore This will give you an idea of the skills or


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.

Learn This section provides a brief discussion of the


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.

Apply This includes questions or blank


sentence/paragraph to be filled into process what

iii
you learned from the lesson.

Assess This is a task which aims to evaluate your level of


mastery in achieving the learning competency.

Answer Key This contains answers to all activities in the


module.

Reflect This contains the learner’s reflection. Learners 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:

Q3_STE_Computer_Programming_ Module 2 Page 1 of 30


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.

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.

Q3_STE_Computer_Programming_ Module 2 Page 2 of 30


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

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

Q3_STE_Computer_Programming_ Module 2 Page 3 of 30


Q3_STE_Computer_Programming_ Module 2 Page 4 of 30
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.

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.

Q3_STE_Computer_Programming_ Module 2 Page 5 of 30


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 changes

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

Q3_STE_Computer_Programming_ Module 2 Page 6 of 30


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

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

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

Q3_STE_Computer_Programming_ Module 2 Page 7 of 30


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

END

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

Q3_STE_Computer_Programming_ Module 2 Page 8 of 30


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.

General Rules in Writing If . . . Else Statement

1. An If statement needs to have at least (1) one relational expression that checks the

relationship between two operands.

Example:

int x = 10; Explanation:

int y = 15; The relational expression == is not the

int z = 10; same as the assignment operator =. The


assignment operator = assigns a value to

x == y // false a variable, constant, array, or vector. The


relational expression checks the
x == z // true
relationship between the two operands.

2. If statement can include multiple relational expressions with logical operators.

Q3_STE_Computer_Programming_ Module 2 Page 9 of 30


// C++ program demonstrating ! logical operator truth
Example:
table
#include <iostream>
using namespace std;

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.

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:

int time = 22; Example explained:

if (time < 10) {


In the example, time (22) is greater than 10,
cout << "Good morning."; first condition is false. The next condition, in
the else if statement, is also false, move on
} else if (time < 20) {
to the else condition since condition1 and
cout << "Good day."; condition2 is both false - and print "Good
evening" to the screen.
} else {

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

to at least have 2 relational expressions. Two logical expressions equate to four

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

expression and use it in If statement.

Example:

// C++ program to find if an integer is even or odd or neither (0)


// using nested if statements using more than 2 logical expressions
#include <iostream>
using namespace std;

int main() {
int num;

cout << "Enter an integer: ";


cin >> 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:

Q3_STE_Computer_Programming_ Module 2 Page 11 of 30

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

programming languages. This allows the program to be analyzed easily and

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

statement may be needed. It is recommended to embed Else statement even if there is

no variable between the Else and Endif keywords.

*** Proficiency in using If . . . Else Statement is a skill to practice when writing a good

program.

Engage

ACTIVITY 1: CODE ME!

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:

Q3_STE_Computer_Programming_ Module 2 Page 12 of 30


Programming
CODE Explanation of the Output
Language

int time = 22; C++ In the example, time (22) is greater

if (time < 10) { than 10, first condition is false. The


next condition, in the else if
cout << "Good morning.";
statement, is also false, move on to
} else if (time < 20) {
the else condition since condition1 and
cout << "Good day.";
condition2 is both false - and print
} else { "Good evening" to the screen.
cout << "Good evening.";

} However, if the time was 14, our

// Outputs "Good evening." program would print "Good day."

Data type used: int, Boolean

Programming
CODE Explanation of the Output
Language

int time = 20; C++


if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}

Q3_STE_Computer_Programming_ Module 2 Page 13 of 30


int time = 20; Java
if (time < 18) {
System.out.println("Good
day.");
} else {
System.out.println("Good
evening.");
}

int time = 20; C#


if (time < 18)
{
Console.WriteLine("Good
day.");
}
else
{
Console.WriteLine("Good
evening.");
}

Apply

Activity 2: If . . . Else statement in a simple program.

INSTRUCTIONS:
1. Write a simple program and apply If . . . Else statement to determine your
grade in Computer Programming.

Q3_STE_Computer_Programming_ Module 2 Page 14 of 30


2. The program will read two (2) types of scores: Written Works (30%) &
Performance Task (70%) and determine the grade based on the following rules:
- If the average >= 75% – 90% => grade = PASS
- If the average score <75% => grade = FAIL
3. You can submit your code via my email address or write it on the space provided.

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

4. The activity will be scored using the Rubrics below:


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.

Q3_STE_Computer_Programming_ Module 2 Page 15 of 30


Assess

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.

Q3_STE_Computer_Programming_ Module 2 Page 16 of 30


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

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

Q3_STE_Computer_Programming_ Module 2 Page 17 of 30


Q3_STE_Computer_Programming_ Module 2 Page 18 of 30
Reflect

Today, I discovered that ____________________________________

_________________________________________________________________

_________________________________________________________________

_________________________________________________________________

_________________________________________________________________

_________________________________________________________________

_______________________.

I encountered difficulties in ________________________________

_________________________________________________________________

_________________________________________________________________

Q3_STE_Computer_Programming_ Module 2 Page 19 of 30

You might also like