STUDENT OUTLINE

Lesson 9 - switch Statements


INTRODUCTION:

We now conclude our coverage of control structures by learning about the multi-way selection tool, switch. An example application of this selection tool appears as the pull-down menus used in current software. Depending on which command is chosen, the program will select one direction out of many. The switch statement in Java allows such decision making.


The key topic for this lesson is:

A. The switch Statements


VOCABULARY:

switch

 

DISCUSSION:

A. The switch Statements

1. The general form of a switch statement is:

switch (expression)
{
case value1:
statement1;
statement2;
...
break;
case value2:
statement3;
statement4;
...
break;
case valuen:
statement;
statement;
break;
default:
statement;
statement;
} /* end of switch statement */

2. The flow of control of a switch statement is illustrated in this diagram:

3. The switch statement attempts to match the integer value of the expression with one of the case integer values. The integer value can only be of data type int, short, byte, or char. No other data types are allowed.

4. If a match occurs, the statements belonging to the case value are executed until the break statement is encountered.

5. The effect of the break statement causes program control to jump to the end of the switch statement. No other cases are executed.

6. A very common error when coding a switch control structure is forgetting to include the break statements to terminate each case value. If the break statement is omitted, all cases following the matching case value are executed. This is usually very undesirable.

7. If no match occurs between the expression and the case values, the optional default choice is executed.

switch (day)
{
  case 1:
    System.out.println( "Monday");
    break;
  case 2:
    System.out.println ("Tuesday");
    break;
  case 3:
    System.out.println ("Wednesday");
    break;
  case 4:
    System.out.println ("Thursday");
    break;
  case 5:
    System.out.println ("Friday");
    break;
  default:
    System.out.println( "not a valid day");
    break;
}

8. The following example applies the switch statement to printing the work day of the week corresponding to a value (1-5):

9. Suppose we wanted to count the occurrences of vowels and consonants in a stream of text. The switch statement is able to analyze character data because such data is converted to integer (ASCII) values for processing.

if (('a' <= letter) && (letter <= 'z'))
switch (letter)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u' :
vowel++;
break;
default :
consonant++;
break;
}

a. Note that multiple case values can be lead to one set of statements.
b. It is good programming practice to include a break statement at the end of the switch structure. If you need to go back and add another case statement at the end of the switch structure, a break statement already terminates the previous case statement.

10. There are programming situations where the switch statement should not replace an if-else chain. If the value being compared must fit in a range of values to result in a statement, the if-else statement should be used.

if  (score >= 90 && score <= 100) 
	grade = 'A';
else if  (score >= 80 && score < 90) 
	grade = 'B';
else if (score >= 70 && score < 80) 
	grade = 'C';

	etc...

You should not replace the above structure with a switch statement.


SUMMARY/ REVIEW:

The programming assignment, L.A.11.1, ACSLLand, will stretch your ability to use control structures effectively. You might want to review the content of the previous lessons before you tackle this problem. Good decomposition into single-purpose methods will make this problem easier to handle and debug.