Module 4 Ste Computer Programming Switch Case Statement
Module 4 Ste Computer Programming Switch Case Statement
Computer
Programming
Quarter III – Module 4:
SWITCH ... CASE STATEMENT
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.
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.
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!
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.
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.
10. The last case for this statement would be the Default
statement,
The activity shows four (4) pictures, you have to guess what the pictures meaning
or symbolises in one word.
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.
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.
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.
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.
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 Case block after the last
Case block, in which case the absence of the Break statement can cause havoc.
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”
Output:
Enter month number: 4
Month name is: April
References
https://beginnersbook.com/2017/08/java-switch-case/