<p>Consider why you might prefer to use a <code>switch</code> in a production environment:</p>

<ol>
<li>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</li>
<li>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 <code>if</code> or <code>else</code>.</li>
<li>Maintainability. I find it safer to add &amp; remove code from a <code>switch</code> statement than an <code>if / else if / else</code> block as I'm more likely to accidentally omit an <code>else</code> from an <code>else if</code> than I am to omit a <code>break</code> from a <code>switch</code>.</li>
<li><p>As per Joshua Bloch's <em>Effective Java</em>: "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:</p>

<pre><code>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;
   }
}
</code></pre></li>
<li>Readability. I find a moderately sized <code>switch</code> statement easier to grok than an equivalent <code>if / else if / else</code> block.</li>
</ol>

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

CC BY-SA 3.0
2017-06-13T04:26:23.293Z
2017-06-13T04:26:23.293Z
Pikalek
<p>The example that instantly comes to mind is Blackjack. See the code excerpt below for an extended example in C (even though it's C, the syntax will pretty much hold true for Java as well):</p>

<pre><code>int cardValue(char card)
{
    int aceValue = -1;

    if (card == 'A')
    {
        do
        {
            printf("What value would you like Ace to be?\n");
            printf("Type 1 or 11: ");
            aceValue = GetInt();
        } while (aceValue != 1 &amp;&amp; aceValue != 11);
    }

    switch (card)
    {
        case '2':
            return 2;
        case '3':
            return 3;
        case '4':
            return 4;
        case '5':
            return 5;
        case '6':
            return 6;
        case '7':
            return 7;
        case '8':
            return 8;
        case '9':
            return 9;
        case 'T':
        case 'J':
        case 'Q':
        case 'K':
            return 10;
        case 'A':
            return aceValue;
        default:
            return -1;
    }
} 
</code></pre>

<p>This function takes in a character corresponding to the card's value. I used a single <code>char</code> as the data type for each card, so I needed a function that took in a card's name (i.e. letter) and returned its value. Were I to design this again, I would probably reduce it down to one <code>return</code> statement and have a variable represent something like <code>cardValue</code> (and in that case I'd need to use <code>break</code> statements, which I currently omit thanks to <code>return</code>). That said, the <code>switch</code> statement is a clear example where you don't want a bunch of <code>if-else</code> statements. For the face cards that would be redundant. There's a nice mix of those cases that "fall through" and those that don't. </p>

<p>Using card games where cards share the same value is a fun, but realistic example for students to see the value of a switch statement. </p>

<p>I'll share one other short example where students have to implement a command-line calculator in C and used <code>switch</code> to identify which operation to perform. Here's the <code>switch</code> from there:</p>

<pre><code>    switch (argv[2][0]) {
        case 'x':
            printf("%f\n", numOne * numTwo);
            break;
        case '/':
            printf("%f\n", numOne / numTwo);
            break;
        case '+':
            printf("%f\n", numOne + numTwo);
            break;
        case '-':
            printf("%f\n", numOne - numTwo);
            break;
        case '%':
            mod = numOne - (numTwo * modNum);
            printf("%f\n", mod);
            break;
        default:
            printf("Usage: &lt;operation&gt; must be x, /, +, -, or %%\n");
            return 1;
    }
</code></pre>

<p>With this example the clarity of the code is made much stronger by virtue of the case statements for each operation. From a readability perspective, it makes it clear how the operations are separated out by showing so visually.</p>

CC BY-SA 3.0
2017-06-12T19:44:49.650Z
2017-06-12T19:44:49.650Z
Peter
<p>Students first see <code>switch</code>/<code>case</code> as a second syntax for <code>if</code>/<code>else</code>, but eventually they should see its strength in creating cascades.  I know of the traditional example of printing a <a href="https://en.wikipedia.org/wiki/Cumulative_song" rel="nofollow noreferrer">cumulative song</a>, like "Twelve Days of Christmas", and below I've placed some code I use to illustrate the difference in use-case between <code>if</code> statements and <code>switch</code> cases.  It nicely prints out all 108 lines of the song <em>Alouette</em> in 44 lines of code.</p>

<p>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.</p>

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

<pre><code>    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");
</code></pre>

CC BY-SA 3.0
2017-06-12T15:04:39.227Z
urrm. why don't you have `break;` in each case? I thought it was necessary...
ItamarG3
CC BY-SA 3.0
2017-06-12T15:06:09.443Z
The choice about where (and if) to use `break`s is the great strength of the structure. If you put a break in every case, you will be back to an `if`/`else` structure. But if you skip some... try running the code =D
Ben I.
CC BY-SA 3.0
2017-06-12T15:09:36.500Z
It will create chains, won't it?
ItamarG3
CC BY-SA 3.0
2017-06-12T15:14:35.240Z
I could tell you, but that would spoil the surprise.  (SPOILER ALERT: yes, it does.)
Ben I.
CC BY-SA 3.0
2017-06-12T15:23:34.243Z
If the students have been introduced to the "clean code" concept, the switch/case construct is easier to read even if there's a `break` for each case. Makes it obvious that it _is_ a multi-branch decision point.
Gypsy Spellweaver
CC BY-SA 3.0
2017-06-26T11:48:23.557Z
From a perspective of someone working in the industry: please don't. Just tell them it was a design mistake in the language. Seemed like a good idea at the time, but turned out to be a huge hurdle in everyday lives of poor Java programmers. Your own example is a good illustration for why it is a bad idea: you have repeated the same code over and over. Obviously, the text of the song won't change, but if you used switch with something that could, you'd again, had a problem with propagating the change through the code (possibly not owned by you).
wvxvw
<lesson-ideas><branching>
Motivating examples for switch/case in Java
# Motivating examples for switch/case in Java

(Asked by: username_0 on 2017-06-12)

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 <= 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");

**Tags:** lesson-ideas, branching

> **Comments:**
> **username_1**: urrm. why don't you have `break;` in each case? I thought it was necessary...  
> **username_0**: The choice about where (and if) to use `break`s is the great strength of the structure. If you put a break in every case, you will be back to an `if`/`else` structure. But if you skip some... try running the code =D  
> **username_1**: It will create chains, won't it?  
> **username_0**: I could tell you, but that would spoil the surprise.  (SPOILER ALERT: yes, it does.)  
> **username_2**: If the students have been introduced to the "clean code" concept, the switch/case construct is easier to read even if there's a `break` for each case. Makes it obvious that it _is_ a multi-branch decision point.  
> **username_3**: From a perspective of someone working in the industry: please don't. Just tell them it was a design mistake in the language. Seemed like a good idea at the time, but turned out to be a huge hurdle in everyday lives of poor Java programmers. Your own example is a good illustration for why it is a bad idea: you have repeated the same code over and over. Obviously, the text of the song won't change, but if you used switch with something that could, you'd again, had a problem with propagating the change through the code (possibly not owned by you).  

---

## 2 answers

### Answer by username_4 on 2017-06-13. Score: 3

Consider why you might prefer to use a switch in a production environment:

Fall through processing. The blackjack example given by @username_5 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 & 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 & 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 & are less likely to get 'coded out' by an mistakenly adding or removing an if or else.
Maintainability. I find it safer to add & 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 & discuss why it was preferable.


---

### Answer by username_5 on 2017-06-12. Score: 2

The example that instantly comes to mind is Blackjack. See the code excerpt below for an extended example in C (even though it's C, the syntax will pretty much hold true for Java as well):
int cardValue(char card)
{
    int aceValue = -1;

    if (card == 'A')
    {
        do
        {
            printf("What value would you like Ace to be?\n");
            printf("Type 1 or 11: ");
            aceValue = GetInt();
        } while (aceValue != 1 && aceValue != 11);
    }

    switch (card)
    {
        case '2':
            return 2;
        case '3':
            return 3;
        case '4':
            return 4;
        case '5':
            return 5;
        case '6':
            return 6;
        case '7':
            return 7;
        case '8':
            return 8;
        case '9':
            return 9;
        case 'T':
        case 'J':
        case 'Q':
        case 'K':
            return 10;
        case 'A':
            return aceValue;
        default:
            return -1;
    }
} 

This function takes in a character corresponding to the card's value. I used a single char as the data type for each card, so I needed a function that took in a card's name (i.e. letter) and returned its value. Were I to design this again, I would probably reduce it down to one return statement and have a variable represent something like cardValue (and in that case I'd need to use break statements, which I currently omit thanks to return). That said, the switch statement is a clear example where you don't want a bunch of if-else statements. For the face cards that would be redundant. There's a nice mix of those cases that "fall through" and those that don't. 
Using card games where cards share the same value is a fun, but realistic example for students to see the value of a switch statement. 
I'll share one other short example where students have to implement a command-line calculator in C and used switch to identify which operation to perform. Here's the switch from there:
    switch (argv[2][0]) {
        case 'x':
            printf("%f\n", numOne * numTwo);
            break;
        case '/':
            printf("%f\n", numOne / numTwo);
            break;
        case '+':
            printf("%f\n", numOne + numTwo);
            break;
        case '-':
            printf("%f\n", numOne - numTwo);
            break;
        case '%':
            mod = numOne - (numTwo * modNum);
            printf("%f\n", mod);
            break;
        default:
            printf("Usage:  must be x, /, +, -, or %%\n");
            return 1;
    }

With this example the clarity of the code is made much stronger by virtue of the case statements for each operation. From a readability perspective, it makes it clear how the operations are separated out by showing so visually.


---

