C Syntax Question
Is it c开发者_StackOverflow社区onsidered 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.
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.
It depends if you have it at the start or the end of the switch statement. If there are other case
s 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;
}
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.
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?
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.
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.
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 case
s, 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.
精彩评论