As
you are learning to program in Java,
there are quite a few logic statements you can use to control the flow of
execution. Obviously, if-then statements are probably the simplest and most
widely used, but there are quite a few other statements that are extremely
powerful once you understand how to use them. One of these statements is known
as the switch statement and it is Java’s
multi-way branch statement.
Using
the switch statement allows you to dispatch execution of your code to different
parts of the program based on the value of a given expression. You can learn
more about using switch statements in your programs in Learn
Java from Scratch.
To
help you understand the construction of a switch statement better, take a look
at the following example which demonstrates the basics of the Java switch statement:
switch
(expression) {
case
value1:
//statement
sequence
break;
case
value2:
//statement
sequence
break;
case
valueN:
//statement
sequence
break;
default:
//default
statement sequence
}
The
expression must be a byte, short, int, or char with each of the value specified
in the case statements being compatible with the expression. String can also be
used as a case expression as long as the case statements are compatible with
the expression. For instance, you cannot add a number to a string value.
How Switch Statements Work
When
the flow of execution in your program reaches the switch statement, the value
of the expression is compared with each of the literal values in the case
statements. If a match is found, the code sequence following that case
statement is executed. If none of the constants match the value of the
expression, the default statement is executed. The default statement is
optional, however, and if no default is present and no case matches the
expression, then no further action is taken and the flow of execution moves
past the switch statement into the next part of the program.
The
break statement within the switch terminates a statement sequence. When your
program encounters a break statement, execution redirects to the first line of
code following the entire switch statement. It may be easier to understand if
you look at the following Java code
example that uses a switch statement to determine the “grade” of an employee
and the associated bonus they receive.
Notice
that in this example, the grade is hardcoded with the statement char Grade =
‘B’;. Typically, the expression will contain variables that are dependent on
other parts of the program as a hard coded variable negates the purpose of
using the switch statement in the first place.
public
class SwitchCaseDemo {
public static void main(String[] args) {
char Grade =’B';
switch (Grade){
case ‘A’:
System.out.println(“You are a Grade A Employee: Bonus= “+ 2000);
break;
case ‘B’:
System.out.println(“You are a Grade B Employee: Bonus= “+ 1000);
break;
case ‘C’:
System.out.println(“You are a Grade C Employee: Bonus= “+ 500);
break;
default:
System.out.println(“You are a Default Employee: Bonus= “+ 100);
break;
}
}
}
In
this example, the output of the program would be “You are a Grade B Employee:
Bonus = 1000.” Of course, this is a simplified example but it is a functional
switch statement that could easily be adopted for more practical use in your
own Java applications.
Nested Switch Statements
The
switch statement can also be used as part of the statement sequence of an outer
switch. This is called a nested switch and is exactly the same idea as a nested
if-then statement. Creating nested switch statements is slightly more complex,
but the good news is that there are no conflicts between the case constants in
the inner switch and those in the outer switch because each switch statement
defines its own block.
You
may find yourself using nested switch statements in many applications as they
require less work than nested if statements and do not have some of the
compatibility issues that can be inherent to complex nested if-then statements.
Important Facts about Switch Statements
There
are a couple of things you should realize when you use switch statements in
your Java applications. For one, the
switch can only check for equality. This means that no other relational
operators (such as greater than or less than) can be used within a case. Case
constants are always evaluated from the top down. This means that the first
case matching the switch expression is the execution entry point. If no break statement is used,
all cases after the entry point will be executed.
Within
the same switch statement, no two case constants can have identical values.
Remember that this is nottrue
if you are using nested switch statements.
Although
it is a best practice to include your default case at the end of your switch
statement, technically it can be located anywhere within the switch statement.
This really comes down to your own personal preference and what makes sense to
you while creating your Java
application. Java
– Make it Your Cup of Coffee covers
switch statements and how to use default cases.
Learning
how to effectively use Java switch
statements can drastically increase the quality of your programs. Not only can
it decrease the amount of code required, but it also is less processor intensive
when you start working on complex desktop applications with thousands of lines
of code.
Remember
that you can use switch statements alone or you can nest them within each other
for very large, complex statements that accomplish multiple tasks simultaneously.
Using
the example in this article, play around with switch statements to learn more
about how they work. Remove the break statement and see how that affects
operation. Although break statements are typically required, there may be
situations where you want all code to be executed after the entry point and you
would intentionally leave break statements out of those particular cases. You
can learn more about the appropriate use of the break statement in the Introduction
to Java Training Course.
Once
you have mastered switch statements, you will be well on your way to becoming a
professional Java programmer that is
capable of creating efficient and powerful applications for the desktop and the
Web.
No comments:
Post a Comment