Skip to main content
Lesson 11 - switch Statements
ZIPPDF (letter)
Lesson MenuPreviousNext
  
The switch Statements page 3 of 7

  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;
        break;
    }		/* 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:  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")l;  break;
      default:  System.out.println( "not a valid day");  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;
      }
    }
    1. Note that multiple case values can lead to one set of statements.
    2. 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, 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.


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.