Question: ```
class switch1
{
    public static void main(String args[])
    {
        int a = 10;
        switch(a)
        {
            default: System.out.println("Default");
            case -1: System.out.println("-1");
        }
    }
}

```

I understand that this program will execute both "default" and "case -1" statements as break is not specified after the matching condition (in this case after "default").

But what I fail to understand is

a) why is `break` needed in a `switch` statement?

b) why does it even execute the invalid matching conditions' statements (i.e executing "case -1")) if all it does is matching?

Answer: 
Sometimes you need multiple cases to execute the same function. For example, I am letting a user specify either mode 1 or mode 32 to represent 32-bit mode, and mode 2 or mode 64 for 64-bit mode.

```
switch (input) {

    case 1:
    case 32:
        /* Do 32-bit related code */
        break;
    case 2:
    case 64:
        /* Do 64-bit related code */
        break;
    default:
        System.out.println("Invalid input");

}

```

This is why breaks are important. They tell the switch statement when to stop executing code for a given scenario. Additionally, the default is generally used for when the switch does not match ANY case.
From the [Java Language Specification section on the `break` statement](http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.15):

> 
> A break statement transfers control out of an enclosing statement.
> 
> 
> 

and...

> 
> a break statement ... always completes abruptly
> 
> 
> 

a) It simply allows transfer of control out of the case statement.

b) The reason why it executes the conditions that aren't what you would expect to be matching is because there are particular conditions where falling through case statements would be considered valid (Taken from [The Java Tutorials](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html)):

```
List<String> futureMonths = new ArrayList<String>();
int month = 8;
switch (month) {
    case 1:  futureMonths.add("January");
    case 2:  futureMonths.add("February");
    case 3:  futureMonths.add("March");
    case 4:  futureMonths.add("April");
    case 5:  futureMonths.add("May");
    case 6:  futureMonths.add("June");
    case 7:  futureMonths.add("July");
    case 8:  futureMonths.add("August");
    case 9:  futureMonths.add("September");
    case 10: futureMonths.add("October");
    case 11: futureMonths.add("November");
    case 12: futureMonths.add("December");
             break;
    default: break;
}

for (String monthName : futureMonths) {
    System.out.println(monthName);
}

```

Which outputs:

> 
> August
> 
> 
> September
> 
> 
> October
> 
> 
> November
> 
> 
> December
> 
> 
>