# Java: switch Statement - Overview

## Purpose of switch: select one of many possible statements to execute

The `if` statement allows you to select one of two sections of code to execute based on a boolean value (only two possible values). The `switch` statement allows you to choose from many statements based on an integer value.

### Equivalent to cascading ifs

A switch can be rewritten with a series of cascading if statements, but in some cases the switch statement is easier to read, and in a some compilers it can produce more efficient code.

## Syntax

    switch (expr) {
      case c1:
            statements // do these if expr == c1
            break;
      case c2:
            statements // do these if expr == c2
            break;
      case c2:
      case c3:
      case c4:         //  Cases can simply fall thru.
            statements // do these if expr ==  any of c's
            break;
      . . .
      default:
            statements // do these if expr != any above
    }

switch
  The switch keyword is followed by a parenthesized integer expression and cases enclosed in braces.. The case corresponding to the value of the expression is executed next. If there is no such case, the default clause is executed. If there is no default clause, execution continues after the end of the switch statement.
case
  The case keyword is followed by an integer constant and a colon. This begins the statements that are executed when the switch expression has the case value.
default
  If no case value matches the expression value, execution continues here. This is the "else" of the switch statement. This is written as the last case be convention. It typically isn't followed by break because execution just continues out the bottom of switch if this is the last clause.
break
  The break statement causes execution to exit to the statement after the end of the switch. If there is no break, execution flows thru into the next case.

## Example - Random comment

    /** Returns a random ambiguous compliment. */
    public static String ambiguousCompliment() {
        String result;   // The generated insult which will be returned
        int which = (int)(Math.random() * 4);  //  Should result in 0 to 3.

        switch (which) {
            case 0:  result = "You look so much better than usual.";
                     break;
            case 1:  result = "Your work is up to its usual standards.";
                     break;
            case 2:  result = "You're quite competent for so litte experience.";
                     break;
            default: result = "Oops -- something is wrong with this code.";
        }
        return result;
    }

Always include a default clause in your switch statement as a general policy of defensive programming - assume there will be bugs in your code and make sure they are caught. In fact the function above does have a bug in it that would be caught by the default clause!

### Where to use switch - not that many places

The ability of `switch` to choose between many sections of code seems to make it more powerful than if. However, selecting sections of code depending on specific integer values turns out not to be very common. If you are handling some specific coded values (eg, the number of the button that was clicked in a JOptionPane), or or processing characters (whose codes are treated like numbers), you will not use it.

## Comments on switch

Java's `if` statement, which was taken directly from C++ to increase its attractiveness to C++ programmers, is not well loved.

- It doesn't allow ranges, eg `case 90-100:`. Many other languages do.
- It requires integers and doesn't allow useful types like String. Many other languages do.
- It is error-prone and a common source of bugs - forgetting break or default silently ignores errors. Some other languages have eliminated these dangerous situations.