Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
549 views

STE Computer Programming Q3 MODULE 3

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

STE Computer Programming Q3 MODULE 3

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

10

Computer
Programming
Quarter III – Module 3:
SWITCH ... CASE STATEMENT

"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: Maricar C. Yturriaga Catarman National High School
Language Editors:
Content Editors: Ma. April E. Estigoy, Pambujan National High School
Illustrator: James Mark D. Acebuche, Magtaon Elementary School
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.

Q2_STE_Computer_Programming_ Module 4 Page 1 of 21


For the learner:

Welcome to the Computer Programming 10 Self–Learning Module 3 on Switch.. Case

Statement!

The hand is one of the most symbolized part 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.

What’s More 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

Q2_STE_Computer_Programming_ Module 4 Page 2 of 21


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 instruction 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 through with it.

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!

Q2_STE_Computer_Programming_ Module 4 Page 3 of 21


Explore

Introduction:

Control statements halt the program execution from going to the next sequential
statement and can shift the execution to another statement, which need not be the
next statement in the sequential order. They assist us in making programmed decisions
and processing the information as necessary.

After going through this module, you are expected to:

Apply the control statement using the Switch… Case in a program.

Sub-Task:

a. Understand Switch . . . Case statements;

b. Discuss the general rules for writing Switch . . . Case statement;

c. Write a simple program that applies Switch . . . Case statements.

Q2_STE_Computer_Programming_ Module 4 Page 4 of 21


PRE-ASSESSMENT

I. TRUE or FALSE. Write TRUE when the statement is correct; write FALSE when the
statement is an error. Write your answers on a separate paper.

1. The last of the statements under each Case would usually be a

“Break” or “Exit”.

2. Each Case statement includes a number of statements.

3. Some programming languages do not allow a range of values with a


special syntax.

4. The Switch case consists of a switching variable defined by the loop


keyword.

5. Most programming languages require a statement to denote the end

of the Switch ... Case statements

6. There would be a number of cases based on the value of the variable.

7. The Case keyword is usually followed by a word, which can either be


numeric or character data.

8. The statements in the Case and Default statements can be action


statements or branching statements.

9. Generally, expressions are not allowed in the Case statements, but


exceptions can always arise.

10. The last case for this statement would be the Default statement,

Q2_STE_Computer_Programming_ Module 4 Page 5 of 21


Learn

In your previous lesson, we studied about the if… else statements.

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

The activity shows four (4) pictures, you have to guess what the pictures meaning or
symbolises in one word.

Q2_STE_Computer_Programming_ Module 4 Page 6 of 21


What is a Switch… Case Statements

A Switch ... Case statement is a control statement in which control is switched


to a practically unlimited number of branches based on the value of a single variable.
Each value of the switching variable would be branched off to a specified set of
statements. When the value of the variable does not match any of the defined cases,
the control is switched off to a set of statements defined under a special case,
“default.” Default is part of the Switch ... Case statement.

The syntax of the Switch ... Case statement is as follows:

Switch <Variable>
Case a
Statement
Statement
Case b
Statement
Statement
Case n
Statement
Statement
Default
Statement
Statement
Endcase

In the earlier statement, a, b, and n are the values for the variable
specified in the Switch statement.

Rules for writing the Switch ... Case statement:

Q2_STE_Computer_Programming_ Module 4 Page 7 of 21


1. The Switch case consists of a switching variable defined by the Switch
keyword.

2. There would be a number of cases based on the value of the variable.


Each value is defined by the Case keyword.

3. The Case keyword is usually followed by a constant, which can either be


numeric or character data.

4. Some programming languages allow a range of values with a special


syntax.

5. Generally, expressions are not allowed in the Case statements, but


exceptions can always arise.

6. Each Case statement includes a number of statements. There is no


restriction on the number of statements that can be under a Case
statement.

7. The last case for this statement would be the Default statement, which
specifies the action to be taken when the value of the variable does not
match any value specified in all the Case statements.

8. The statements in the Case and Default statements can be action


statements or branching statements.

9. Most programming languages require a statement to denote the end of


the Switch ... Case statements. Some would define a keyword, such as
Endcase. Some programming languages, such as in the C-family, just use

Q2_STE_Computer_Programming_ Module 4 Page 8 of 21


a closing curly brace “}” to denote the ending of the Switch ... Case
statement.

10. The last of the statements under each Case would usually be a “Break”
or “Exit” statement. It would be usually the only word in that statement.
The execution of Break (or Exit) statement would take the execution to
the statement that immediately follows the Endcase statement.

The total set of statements, beginning with the first Case


statement and ending with the Endcase (or its equivalent) statement, are
usually called the Switch Block. The statements in each of the Case
statements are usually called the Case Block.

Utility of Switch ... Case Statement

Switch ... Case statement is used when there is a need for making a decision
based on one condition that can have multiple outcomes. We use If statement when the
decision has one or two possible outcomes. When the possible outcomes are numerous,
we use Switch ... Case statements. When converting from numbers to strings, this
statement comes in handy. Switch ... Case statements also come in handy when we are
developing software packages such as MS-Word, where in the character being typed in
can lead to any one of a variety of different actions.

Precautions for Using Switch ... Case Statement

It is essential to use the Break (or Exit) statement at the end of the statements
in every Case block. Most often, programmers tend to neglect the Break statement in
the last block of Case statements. It is also common to forget the Break statement in
the Default block of statements. Break forces the execution to go outside the Switch
block of statements. While writing computer programs, we need to remember that the
program will certainly undergo modification or enhancement. We are likely to add a

Q2_STE_Computer_Programming_ Module 4 Page 9 of 21


Case block after the last Case block, in which case the absence of the Break statement
can cause havoc.

Another mistake programmers often commit is forgetting to include the Default


block. They possibly think that all possible outcomes of the decision scenario are
covered, but real life is so weird that it can throw up incredible values at the programs
resulting in unexpected aborts. So, it is essential to include the Default block in every
Switch ... Case statement.

Dataflow of a Switch… Case Statements.

Q2_STE_Computer_Programming_ Module 4 Page 10 of 21


Usually the conversion of numbers to strings of characters or vice versa is
achieved by a Switch ... Case statement. Let us consider an example for converting
month number to month name:

Switch MonthNumber
Case 1
MonthString = “January”
Break
Case 2
MonthString = “February”
Break
Case 3
MonthString = “March”
Break
Case 4
MonthString = “April”
Break
Case 5
MonthString = “May”
Break
Case 6
MonthString = “June”
Break
Case 7
MonthString = “July”
Break
Case 8
MonthString = “August”
Break
Case 9
MonthString = “September”
Break
Case 10
MonthString = “October”
Break
Case 11
MonthString = “November”
Break
Case 12
MonthString = “December”

Q2_STE_Computer_Programming_ Module 4 Page 11 of 21


Break
Default
MonthString = “Unclassified”
Break
Endcase

Output:
Enter month number: 4
Month name is: April

Engage
Activity 1: Name 3 programming languages that uses the Switch… Case Statement and
write their syntax. Use the table below in answering.

Programming Language Programming Language Programming Language


Name: Name: Name:

Syntax: Syntax: Syntax:

Q2_STE_Computer_Programming_ Module 4 Page 12 of 21


Apply

Activity 2: Understanding a Switch… Case Statement Program

After determining the programming languages that uses the switch…case


statement, select one (1) programming language and code the following simple
switch… case program.

1. Write a simple program and apply the switch…case statement in determining


the grade of student with a descriptive value, whereas;

95 – 100 Outstanding

90 – 94 Very Satisfactory

85 – 89 Satisfactory

80 – 84 Very Good

75 – 79 Good

60 – 74 Failed

2. Write a simple program that display the name of the week according
to the number that corresponding the day.

To score these activity rubrics is being used. Follow the rubrics criteria for your
guidance.

Q2_STE_Computer_Programming_ Module 4 Page 13 of 21


CRETERIA POINTS SCORE
Accuracy
The output of the program meets the specified
specification.
Code Efficiency
Code appropriately uses the Switch…case
statement in the specified Programming
Languages
Timeliness
Finished and submitted on the schedule time.
Total

Assess

Programming Language Name:

Code Start Here!

Q2_STE_Computer_Programming_ Module 4 Page 14 of 21


Reflect

Today, I discovered that

I encountered difficulties in

Q2_STE_Computer_Programming_ Module 4 Page 15 of 21

You might also like