0% found this document useful (0 votes)
10 views6 pages

Module 8 Switch Statements

This module introduces the Java switch statement, a control structure that allows decision-making based on multiple conditions, providing a clearer alternative to nested if-else statements. Students will learn the syntax, implementation, and behavior of switch statements, including the use of break and default keywords, as well as the fall-through behavior. The module includes examples and important points regarding the limitations and proper usage of switch statements in Java.

Uploaded by

saringanervie6
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)
10 views6 pages

Module 8 Switch Statements

This module introduces the Java switch statement, a control structure that allows decision-making based on multiple conditions, providing a clearer alternative to nested if-else statements. Students will learn the syntax, implementation, and behavior of switch statements, including the use of break and default keywords, as well as the fall-through behavior. The module includes examples and important points regarding the limitations and proper usage of switch statements in Java.

Uploaded by

saringanervie6
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

DCIT 22 – Computer Programming 1

Module 8: Switch Statement

Introduction:
In this module, students will explore the fundamental concepts of the Java
switch statement, a powerful control structure that allows decision-making based on
multiple conditions. Unlike nested or multiple if-else statements, the switch statement
provides a more structured and readable approach for evaluating a single variable
against multiple values. It enhances program clarity, especially when dealing with a
large number of specific, discrete values.

Objectives:
In this module the student should be able to:
1. Understand the syntax and structure of the Java switch statement.
2. Implement switch statements to handle multiple case scenarios effectively.
3. Use the break and default keywords appropriately to control program flow
within a switch block.
4. Recognize the fall-through behavior of switch statements and apply it when
necessary.
5. Solve problems that require decision-making logic using Java switch
statements.

Java Switch Statement


Java switch statement allows a variable to be tested for equality against a list
of values. Each value is called a case, and the variable being switched on is checked
for each case.

The switch statement can be used when multiple if-else statements are
required. It can have multiple code blocks along with the case values and execute one
of many code blocks based on the matched case value.

In other words, the switch statement tests the equality of a variable against multiple
values.

Syntax:

Cavite State University – Naic


Information Technology Department
DCIT 22 – Computer Programming 1
Module 8: Switch Statement

Points to Remember:
o There can be one or N number of case values for a switch expression.
o The case value must be of switch expression type only. The case value must
be literal or constant. It doesn't allow variables.
o The case values must be unique. In case of duplicate value, it renders compile-
time error.
o The Java switch expression must be of byte, short, int, long and string.
o Each case statement can have a break statement which is optional. When
control reaches to the break statement, it jumps the control after the switch
expression. If a break statement is not found, it executes the next case.
o The case value can have a default label which is optional.

In Java, switch statement mainly provides a more detailed alternative that avoids the
usage of nested or several if-else statements when associated with an individual
variable.

The syntax of the Java switch statement contains the switch keyword which is
followed by the expression that needs to be evaluated using parentheses. The
mentioned expression must definitely evaluate to a definite data type which is primitive
such as int, char.

Flowchart:

Cavite State University – Naic


Information Technology Department
DCIT 22 – Computer Programming 1
Module 8: Switch Statement

The default keyword


In Java, the switch statement can also contain a default label. The default label
will be executed only in the situation when none of the case labels are matching the
expressions value. The declaring of default label is considered optional, but can be
useful in the events of unexpected values or inputs.

The break keyword


When Java reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for
more testing.

Example 1:
The example below uses the weekday number to calculate the weekday name:

Cavite State University – Naic


Information Technology Department
DCIT 22 – Computer Programming 1
Module 8: Switch Statement

Output:
Thursday

Fall-through behavior in a switch statement occurs when a case block does not
contain a break statement, causing the program to continue executing subsequent
case blocks, regardless of whether their conditions match the switch expression.
This behavior allows multiple cases to share the same code block or enables
sequential execution of case blocks until a break statement or the end of the switch
block is reached.

Example 2 (Fall-through):
In this example, we're showing use of switch statement where cases are based on a
char. We've created a variable grade. Based on value of grade, each case is
checked. if a case is satisfied and break statement is present then following cases
are not checked.

Output:
EWell done
Your grade is Ba positive number.

Cavite State University – Naic


Information Technology Department
DCIT 22 – Computer Programming 1
Module 8: Switch Statement

Example 3 (Nested Switch Case):


In this example we will determine the courses taken by a student depending on the
college year level and the ‘branch’ or the course program.

Cavite State University – Naic


Information Technology Department
DCIT 22 – Computer Programming 1
Module 8: Switch Statement

Output:
Enter a positive or negative number
Data Communication and Networks, MultiMedia
integer: [value] is a positive number.

Important Points About Java Switch Statement


o One of the major important features about Java Switch statement is it's fall
through behavior. It means that in case a case label does not contain a break
statement, then the execution will be passed on directly to the next case label. A
switch statement can also include a default label, which is executed if none of the
case labels match the expression's value. The default label is optional but can be
useful for handling unexpected or unspecified values.
o Switch statements can only match exact values, and they cannot check ranges of
values. This means that if you need to check for a range of values, you would
need to use multiple case labels or resort to other control flow statements like if-
else.
o Switch statements can only be used to check for equality between the expression
and the case labels. They cannot perform more complex checks, such as
checking conditions or using comparison operators.

REFERENCES:

Java Point. (n.d.). Java Switch. Retrieved December 2024, from


[Link]

Tutorials Point. (n.d.). Java Switch Statement. Retrieved December 2024, from
[Link]

W3Schools. (n.d.). Java Switch. Retrieved December 2024, from


[Link]

Cavite State University – Naic


Information Technology Department

You might also like