APCS o Lesson 11

Computer Science - C++

Lesson 11 - 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 C++ 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.

 

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.

 


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

 

switch (day)

{

case 1:  cout << "Monday" << endl;  break;

case 2:  cout << "Tuesday" << endl;  break;

case 3:  cout << "Wednesday" << endl;  break;

case 4:  cout << "Thursday" << endl;  break;

case 5:  cout << "Friday" << endl;  break;

default:  cout << "not a valid day" << endl;  break;

}

 

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.

 

11.  Finally, the switch statement cannot compare double values.

 

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 Lessons 6-10 before you tackle this problem.  Good decomposition into single-purpose functions will make this problem easier to handle and debug.

 

 

ASSIGNMENT:                     Lab Exercise, L.A.11.1, Grades

                                                 Lab Exercise, L.A.11.2, ACSLLand - Intermediate Division

                                                 Lab Exercise, L.A.11.3, ACSLLand - Senior Division