switch Statement

A switch statement is useful when you need to select one of several alternatives based on the value of an integer, a character, or a String variable. The basic form of the switch statement is this:

switch (expression)

{

case constant:

statements;

break;

[ case constant-2:

statements;

break; ] ...

[ default:

statements;

break; ] ...

}

The expression must evaluate to an int, short, byte, or char. It can’t be a long or a floating-point type.

Each grouping of code lines that starts with the case keyword and ends with a break statement is a case group. You can code as many case groups as you want or need. Each group begins with the word case, followed by a constant (usually, a numeric, character, or string literal) and a colon. Then you code one or more statements that you want executed if the value of the switch expression equals the constant. The last line of each case group is a break statement, which causes the entire switch statement to end.

The default group, which is optional, is like a catch-all case group. Its statements are executed only if none of the previous case constants matches the switch expression.

tip.eps The case groups are not true blocks marked with braces. Instead, each case group begins with the case keyword and ends with the case keyword that starts the next case group. All the case groups together, however, are defined as a block marked with a set of braces.

Warning.eps The last statement in each case group usually is a break statement. A break statement causes control to skip to the end of the switch statement. If you omit the break statement, control falls through to the next case group. Accidentally leaving out break statements is the most common cause of trouble with using a switch statement.

Here’s an example of a switch statement that assigns a value to a variable named commissionRate based on the value of an integer variable named salesClass:

double commissionRate;

switch (salesClass)

{

case 1:

commissionRate = 0.02;

break;

case 2:

commissionRate = 0.035;

break;

case 3:

commissionRate = 0.05;

break;

default:

commissionRate = 0.0;

break;

}

The switch statement can also evaluate char data. In the following example, a char variable named salesCategory is evaluated to assign commission rates. The possible sales categories are A, B, or C. However, the category codes may be uppercase or lowercase:

double commissionRate;

switch (salesCategory)

{ case ‘A’:

case ‘a’:

commissionRate = 0.02;

break;

case ‘B’:

case ‘b’:

commissionRate = 0.035;

break;

case ‘C’:

case ‘c’:

commissionRate = 0.05;

break;

default:

commissionRate = 0.0;

break;

}

The key to understanding this example is realizing that you don’t have to code any statements at all for a case group, and that if you omit the break statement from a case group, control falls through to the next case group. Thus, the case ‘A’ group doesn’t contain any statements, but it falls through to the case ‘a’ group.

Beginning with Java 7, you can also use string values in a switch statement. For example:

double commissionRate;

switch (salesCategoryName)

{

case “Category A”:

commissionRate = 0.02;

break;

case “Category B”:

commissionRate = 0.035;

break;

case “Category C”:

commissionRate = 0.05;

break;

default:

commissionRate = 0.0;

break;

}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset