Motivating examples for switch/case in Java Students first see switch/case as a second syntax for if/else, but eventually they should see its strength in creating cascades.  I know of the traditional example of printing a cumulative song, like "Twelve Days of Christmas", and below I've placed some code I use to illustrate the difference in use-case between if statements and switch cases.  It nicely prints out all 108 lines of the song Alouette in 44 lines of code.

The only other motivating example I can think of is a graduated tax calculator (based on the US Federal Income Tax), where cascades can be a nice way to approach the problem.

These examples are fine, but it's not enough to get the lightbulb to flick on for most of my students.  What I need is a few more motivating examples that really show students why switch/case is useful.  What other examples show this clearly?

    System.out.println("Alouette, gentille alouette,\n"
            + "Alouette, je te plumerai.\n");

    for(int verse = 1; verse &lt;= 8; verse++){
        if(verse == 1){
            System.out.println("Je te plumerai la tête.\nJe te plumerai la tête.");
        } else if (verse == 2){
            System.out.println("Je te plumerai le bec.\nJe te plumerai le bec.");
        } else if (verse == 3){
            System.out.println("Je te plumerai les yeux.\nJe te plumerai les yeux.");
        } else if (verse == 4){
            System.out.println("Je te plumerai le cou.\nJe te plumerai le cou.");
        } else if (verse == 5){
            System.out.println("Je te plumerai les ailes.\nJe te plumerai les ailes.");
        } else if (verse == 6){
            System.out.println("Je te plumerai les pattes.\nJe te plumerai les pattes.");
        } else if (verse == 7){
            System.out.println("Je te plumerai la queue.\nJe te plumerai la queue.");
        } else if (verse == 8){
            System.out.println("Je te plumerai le dos.\nJe te plumerai le dos.");
        }

        switch(verse){
            case 8:
                System.out.println("Et le dos! Et le dos!");
            case 7:
                System.out.println("Et la queue! Et la queue!");
            case 6:
                System.out.println("Et les pattes! Et les pattes!");
            case 5:
                System.out.println("Et les ailes! Et les ailes!");
            case 4:
                System.out.println("Et le cou! Et le cou!");
            case 3:
                System.out.println("Et les yeux! Et les yeux!");
            case 2:
                System.out.println("Et le bec! Et le bec!");
            case 1: 
                System.out.println("Et la tête! Et la tête!");
            default: 
                System.out.println("Alouette! Alouette!\n"
                        + "Ah-ah-ah-ah\n");
        }

        System.out.println("Alouette, gentille alouette,\n"
                + "Alouette, je te plumerai.\n");
Consider why you might prefer to use a switch in a production environment:


Fall through processing. The blackjack example given by @Peter is a good example of this because it mirrors how we think about the problem: all of the face cards fall into the same case &amp; the code follows suit. Be careful with this though, it's easy to abuse this to get terser code at the expense of readability
Default handlers. Personally, I find this most useful not in a 'single moment of code', but over a series of refinements. For instance, it's common to not have all your logic to handle multiple situations in place when you start coding, but it's also easy to miss a special case that you meant to add later. A default handler is a convenient &amp; concise way to stub out code that you haven't yet gotten to.  Default handlers for that throw an exceptions or log the event can help avoid silent failures &amp; are less likely to get 'coded out' by an mistakenly adding or removing an if or else.
Maintainability. I find it safer to add &amp; remove code from a switch statement than an if / else if / else block as I'm more likely to accidentally omit an else from an else if than I am to omit a break from a switch.
As per Joshua Bloch's Effective Java: "Switches on enums are good for augmenting external enums types with constant-specific behavior." For example if you cannot change the source for an Operation enum, but need add support for an inverse method:

public static Operation inverse(Operation op){
   switch(op){
      case PLUS:   return Operation.MINUS;
      case MINUS:  return Operation.PLUS;
      case TIMES:  return Operation.DIVIDE;
      case DIVIDE: return Operation.TIMES;
   }
}

Readability. I find a moderately sized switch statement easier to grok than an equivalent if / else if / else block.


When possible, avoid contrived examples for motivating usage. Instead, show where you (or someone else) used a switch &amp; discuss why it was preferable.