0% found this document useful (0 votes)
86 views23 pages

STE - Computer Programming Q3 MODULE 2 - 094143

Computer programming

Uploaded by

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

STE - Computer Programming Q3 MODULE 2 - 094143

Computer programming

Uploaded by

iamgilverttt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

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@deped.gov.
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.

For the learner:

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.

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.
This comprises activities for independent
Engage
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.
This is a task which aims to evaluate your level
Assess
of mastery in achieving the learning competency.

This contains answers to all activities in the


Answer Key module.

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:

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.

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.

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.

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

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 2 of 23


______________________________

_______________________________ _______________________________

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

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

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

Q3_STE_Computer_Programming_ Module 2 Page 4 of 23


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.

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

Q3_STE_Computer_Programming_ Module 2 Page 5 of 23


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

We can use these conditions in performing different actions


END 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 6 of 23


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
a variable, constant, array, or vector. The
x == y // false relational expression checks the
x == z // true relationship between the two operands.

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

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:

int time = 22; Example explained:


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

} else { However, if the time was 14, our program


would print "Good day."
cout << "Good evening.";

} Data type used: int

// Outputs "Good evening."

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; Page 8 of 23
Q3_STE_Computer_Programming_ Module 2
int main() {
int num;

cout << "Enter an integer: ";


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.

Q3_STE_Computer_Programming_ Module 2 Page 9 of 23


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:

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

Q3_STE_Computer_Programming_ Module 2 Page 10 of 23


int time = 20; C++
if (time < 18) {
cout << "Good day.";
} else {
cout << "Good
evening.";
}

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

Q3_STE_Computer_Programming_ Module 2 Page 11 of 23


Assess

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.
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.
4. The activity will be scored using the Rubrics below:
CRITERIA 1 5 6 8 10
ACCURACY The output is The output has The output has The output has The output has
totally shown 4 – 5 shown 2 – 3 shown 1 error. no error/s.
incorrect. 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 – 5 Submitted 1 – Submitted on
at all. week or more days after the 2 days after or before the
after deadline. deadline. deadline. deadline.

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.

Q3_STE_Computer_Programming_ Module 2 Page 12 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.

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

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 13 of 23


Reflect

Today, I discovered that ____________________________________


Q3_STE_Computer_Programming_ Module 2 Page 14 of 23
_________________________________________________________________

_________________________________________________________________

_________________________________________________________________

_________________________________________________________________

_________________________________________________________________

_______________________.

I encountered difficulties in ________________________________

_________________________________________________________________

_________________________________________________________________

_________________________________________________________________

_________________________________________________________________

_________________________________________________________________

_________________________________________________________________

_________________________________________________________________

___________________________.

Answer Key

Q3_STE_Computer_Programming_ Module 2 Page 15 of 23


Page 16 of 23 Q3_STE_Computer_Programming_ Module 2
York: CRC Press, 2019), 74 - 76
Murali Chemuturi, Computer Programming for Beginners: A step by step Guide (New
References
Asses
Apply
TEST I.
1. IF
The activity will be rated using the Rubrics below:
2. ELSE
3. ELSE CRITERIA 1 5 6 8 10
4. IF ACCURACY The output The output The output The output The output
5. IF is totally has shown has shown has shown has no
6. IF incorrect. 4 – 5 errors. 2 – 3 errors. 1 error. error/s.
7. ELSE CODE EFFICIENCY Code does Codes Codes Codes Code
8. IF not work. generate generate generate generates
9. ELSE output with output with output with the output
10. ELSE 4 – 5 error. 2 – 3 error. 1 error. desired.
TIMELINESS Did not Exceeded a Submitted Submitted Submitted
submit at week or 3 – 5 days 1 – 2 days on or before
TEST II. all. more after after the after the
Common deadline. deadline. deadline. deadline.
Answers:
TOTAL
JavaScript,
Java, C, C++,
Engage Explore
PROGRAMMING LANGUAGE: TEST I.
1. C++
2. Java 1. IF
3. C# 2. ELSE
*EXPLANATION will be scored using a Rubrics. 3. ELSE
4. IF
5. IF
6. IF
7. ELSE
8. IF
9. ELSE
10. ELSE
TEST II. Common Answers:
JavaScript, Java, C, C++,
Visual Basic, C#
“C++ If . . .Else,” Accessed January 4, 2021,
https://www.w3schools.com/cpp/cpp_conditions.asp

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


https://www.computerhope.com/jargon/c/contstat.htm

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


https://www.programiz.com/cpp-programming/if-else

Q3_STE_Computer_Programming_ Module 2 Page 17 of 23


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@deped.gov.ph


*clmd.region8@deped.gov.ph *lrmds.region8@deped.gov.ph

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy