Should the default case of switch statment have a break?

Example from Oracle

public class SwitchDemo {
    public static void main(String[] args) {

        int month = 8;
        String monthString;
        switch (month) {
            case 1:  monthString = "January";
                     break;
            case 2:  monthString = "February";
                     break;
            case 3:  monthString = "March";
                     break;
            case 4:  monthString = "April";
                     break;
            case 5:  monthString = "May";
                     break;
            case 6:  monthString = "June";
                     break;
            case 7:  monthString = "July";
                     break;
            case 8:  monthString = "August";
                     break;
            case 9:  monthString = "September";
                     break;
            case 10: monthString = "October";
                     break;
            case 11: monthString = "November";
                     break;
            case 12: monthString = "December";
                     break;
            default: monthString = "Invalid month";
                     break; //why is there a switch statement here?
        }
        System.out.println(monthString);
    }
}

The page explains

Technically, the final break is not required because flow falls out of the switch statement. Using a break is recommended so that modifying the code is easier and less error prone. The default section handles all values that are not explicitly handled by one of the case sections.

I'm not sure if the second sentence is justifying the use of breaks in switch statements in general or is specifically saying adding a break statement to the end makes code less error prone. If I'm throwing an exception in the default case is there a point to calling break afterwards or is this a waste? In Netbeans a red ! appears beside the unreachable break statement but it compiles fine.

Regarding readability, how should a case block be formatted if the code that runs when the condition is met spans several lines? For example instead of just monthString = "January"; there's 10 lines of code with loops and things?

I'm using the switch to test if the user entered the number for option 1, 2 or 3.


I'm not sure if the second sentence is justifying the use of breaks in switch statements in general or is specifically saying adding a break statement to the end makes code less error prone.

The latter.

If I'm throwing an exception in the default case is there a point to calling break afterwards or is this a waste?

Waste. Non-newbies recognize that throwing an exception is a de facto break. Making code robust is one thing, trying to bulletproof it against beginners and morons is a waste of time. They'll justf ind other ways to break it.

In Netbeans a red ! appears beside the unreachable break statement but it compiles fine.

Yeah but you don't want those red !s. Get rid of it.

Regarding readability, how should a case block be formatted if the code that runs when the condition is met spans several lines? For example instead of just monthString = "January"; there's 10 lines of code with loops and things?

Consider putting it in a separate method.

I'm using the switch to test if the user entered the number for option 1, 2 or 3.

In that case, do you need a default at all? It's not required in a switch.

I think default is a really bad idea in most cases. The exception would be where it's possible to have invalid values passed to the switch statement and you need to do something about that (throw an exception, display a message, whatever). Otherwise, every possible value should have a case, even if it's empty, so that it's clear that the value is valid but that you intended for nothing to happen.


If you throw an exception in the default case the break will never get executed so you can leave it away then. I personally use braces to avoid bad readability when dealing with switches in general.

So for instance:

switch (variable) {
   case 1: {
       code;
       mode code;
       break;
   }
   default: {
       break;
   }
}

Without the braces but instead good indented it's also well readable. Example:

    switch (variable) {
       case 1: 
           code;
           mode code;
           break;
       default: {
           break;   
    }

While it's not required (because it will exit without the break statement just the same) I think it's a good habit to include a break; in your default case. Often times it's when we start thinking we know it all that we'll end up introducing a bug that could have easily been prevented had we stuck with the basics. It's a matter of consistency . It's similar to using braces for single line condition statements like so:

 if(true)
     do x;
 else
     do y;

vs

if(true) {
    do x;
} else {
    do y;
}

Take for instance apple's well known SSL/TLS bug https://www.imperialviolet.org/2014/02/22/applebug.html

链接地址: http://www.djcxy.com/p/84472.html

上一篇: 在C ++中切换语句行为

下一篇: 开关状态的默认情况是否有中断?