switch Java Help


The switch statement is Java's multiply branch statement. It provides an easy way to  dispatch execution to different part of your code based on the value of an expression. As such, it often provides a better alternative than a target series of if-else-if statements. Here is the general form 01 a switch statement:

switch(expression) {
case valuel:
/ / statement sequence.
break; .
case value2:
l/ statement sequence



case valueN:
/ / statement.sequence
break; .
/ / default statement sequence

The exasperation must be)(of type( byte .short, into or char; each of the.values specified i.n the case statement must be of a type compatible with the expression. Each case value must be a unique literal (that is. it must be a constant, not a variable). Duplicate . case values are not allowed The switch statement works like this: 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 matches' the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken: The break statement is used inside the switch to terminate a statement sequence. When 'a break statement is encountered, execution branches to the first line of code that
follows the entire switch statement. This has. the effect of "jumping out" of the switch.  Here is a simple example that uses a switch statement:

II A simple example of the switch.
class SampleSwitch {
public static void main(String ,args[]I
for(int i'=O; x<6; ,i++)
switch(i) {
case 0:
SY,stem.out'.println (" i is zero.");
case 1:
. System;out.println("j is one.");
,case 2:
System,out.println("i is two,");'
case' 3: .
System,out,println{"i is three.");
System.out.println("i is greater than 3.");

The output produced by this program is shown here:

i is zero.
,i is one.
i is two. "ri
is three. .".
i is greater. than 3.
;: "
i is greater than 3,

As you can see, each time through the loop, the statements associated with the case'
constant that matches are executed. All others are bypassed. After: i is greater than 3,' , no case statements match, so the default statement is executed. The break statement of is optional. If you omit the break, execution will continue on into the next case. It is sometimes desirable to have multiple cases without break statements between them. For example, consider the following program.

II In a switch, break statements are optional.
class MissingBreak (
public static void main(String args[),{ :
fpr'(int i=O; i<1-2,; i++)
,switch(i) (
case 0:
case 1:
'case 2:
case 3:,
'case 4:
System.out.println("i is less than 5"):
case 5:
case 6:
case 7:
'case 8:
system.out.println(i is less then 10 );
system.out:print("i ,is 10 or more") i

This Program generates the following output

i  less than 5
i is less than 5
i is less  than 5
i is less than'5
i is less than 5
i  is less than 10
i is less than 10
i is  less than  10
i is less 'than 10
i is less than 10
i is 10 or more
i is 10 or more

As you can see, execution falls through each case until a break statement (or the end of While the preceding example is, of course, 'contrived for the sake of illustration,
omitting the break statement.has 'many practical applications in real programs. To
sample its more' realistic usage, consider the following rewrite of the season example shown earlier. This version uses a switch to provide a more efficient, implementation.

// An improved version of the season program.
class Switch {
public static void main(String args[]) {.
in.t month = 4;
String season;
switch {month)
case 12:
case 1:
case 2:

season = Winter
case 3:
case 4:
case 5:
season = / Spring
case 6:
case 7:
case 8:
season = Summer
case 9:
case 10:
case 11 :

Posted on September 16, 2014 in The Complete Reference

Share the Story

About the Author

Back to Top
Share This