4

Is it considered bad coding to put a break in the default part of a switch statement? A book I was reading said that it was optional but the teacher counted off for using it.

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51
shinjuo
  • 20,498
  • 23
  • 73
  • 104
  • 6
    Very odd to lose points for that. It's unnecessary, but pretty harmless. – Michael Berkowski Sep 19 '11 at 20:12
  • It's not needed but it does no harm either. Might be handy to have if you end up rearranging your `case` clauses and the `default` clause. Your teacher may have thought you didn't know it wasn't necessary. – Tom Zych Sep 19 '11 at 20:13
  • Its an online class which is basically just reading the book and the teacher grades the assignments. The book itself said in a side bar it was optional and the teacher never said otherwise until the assignment was graded – shinjuo Sep 19 '11 at 20:15
  • 3
    The language specification does not require that default case be written last, it can be written prior to other cases, or freely mixed in the midst of them. Your instructor and the book you have consulted both sound wrong. The book gives bad advice to consider not writing the statement where you intend it and the instructor even worse. Don't argue with the instructor, just don't believe everything this person tells you. – Heath Hunnicutt Sep 19 '11 at 20:19
  • CS teachers (especially TA's) can be so blind to the forest for all the trees sometimes. Reminds me of an assignment I had back in the day that I lost a full letter grade (A->B) for a misspelling in a code comment. – JohnFx Sep 19 '11 at 20:24
  • 1
    You might ask your teacher if comments are required in code. After all, they are optional (the compiler ignores them). Perhaps he has no interest in readable indentation either? – Ned Batchelder Sep 19 '11 at 21:50
  • Using break; in the last case of a switch is optional, but there is no requirement that default be the last case. – DwB Sep 21 '11 at 18:00

7 Answers7

15

Best practice is to always use a break unless you mean for control to flow into another case, even at the end of the switch.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
8

It depends if you have it at the start or the end of the switch statement. If there are other cases after the default then you probably do want a break there unless you really want to fall through.

switch (a)
{
    case 0:
    default:
        printf("Default\n");
        break;

    case 1:
        printf("1\n");
        break;

    case 2:
        printf("2\n");
        break;
}
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
5

The teacher was being extremely pedantic, penalizing students for putting something in the source code which makes no difference at all.

It's true that there is no real reason to put a break in a statement like this:

switch(i) {
   case 0:
      // whatever
      break;
   default:
      // whatever
}

But:

It's a good idea to put a break after all case statements. What if another statement is later added to the switch?

switch(i) {
   case 0:
      // whatever
      break;
   default:
      // whatever
   case 1:
      // OOPS! Is the missing break above intentional or a mistake?
}

Even if someone argues that this should never happen (which is quite a strong thing to say IMHO), you should ask your teacher why he counted off for this and not, for example, for using superfluous whitespace in the source. After all, whitespace doesn't change the meaning of the program as well.

Jon
  • 428,835
  • 81
  • 738
  • 806
3

I've worked in places where break was required in every case of a switch, including the default case. Did your teacher explain why he or she marked it wrong?

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • It's probably not worth antagonizing the person who's going to grade your future assignments, but while it may not be needed, it's also not wrong, and it's often considered better style to use it. BTW, braces around the body of each case aren't needed either, but they're also considered good style by many. – Caleb Sep 19 '11 at 20:26
2

I agree with everybody else: it's not necessary iff the default follows the final case. But I always put it in anyway because I (or, worse, some other poor brute) might inadvertently add another case following the default six months down the road. Sure, that would be an error, but only a typing error: why should I leave the code vulnerable to a hard-to-find bug1 just because I made some dumbass fumble-fingered midnight mistake? Who needs that kind of grief?

Note to instructor: inserting the break in this circumstance is an example of defensive code, which is a very good thing always.

Note to student: grind your teeth, grimace and groan, have a beer, and move on, always remembering to avoid this instructor next semester.

1 This actually happened in a project I was on and it took us [sic! us: the whole project team] all afternoon to find the bug typing error; we looked everywhere except the switch block.

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51
1

It is not necessary if the default case is the last case.

If one day you decide to move the last case at an other place, it may save you some bugs if there was a break.

The rest is a matter of taste. It's not bad coding.

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
0

The default branch should always be the last case within the switch block, so with or without break, execution continues after the switch. So often break isn't used there, because it is superfluous.

However, if someone decides to change the order of cases, the existence or lack of break may make a huge difference. But then again, moving the default case in front of another one would be a serious mistake by itself.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • Not my downvote, but I don't agree on the `default` having to be last. Personally I find it intuitive to sometimes write `default: case "foo":` etc (with newlines, but you get the idea). – Jon Sep 19 '11 at 20:19
  • Not me, but *should always* and *is always* aren't the same thing. The only time the default case *needs* to be at the end is if you don't put a `break` in your default case, so the logic is somewhat circular. If, OTOH, you put a `break` in every case, it doesn't much matter where the default case goes, except for style. – Caleb Sep 19 '11 at 20:23
  • @PéterTörök, that must be why you're smiling in your picture. – Caleb Sep 19 '11 at 20:35