简体   繁体   English

C语法问题

[英]C Syntax Question

Is it considered bad coding to put a break in the default part of a switch statement? 在switch语句的默认部分中断是否被认为是错误的编码? A book I was reading said that it was optional but the teacher counted off for using it. 我正在阅读的一本书说它是可选的,但老师因使用它而被计算在内。

最佳做法是始终使用break除非您指的是控制流入另一个案例,即使在交换机结束时也是如此。

It depends if you have it at the start or the end of the switch statement. 这取决于你是否在switch语句的开头或结尾处有它。 If there are other case s after the default then you probably do want a break there unless you really want to fall through. 如果在default case还有其他case ,那么你可能希望在那里break ,除非你真的想要通过。

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: 这是真的,有没有真正的理由把一个break在这样的语句:

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

But: 但:

It's a good idea to put a break after all case statements. 在所有案例陈述之后break是个好主意。 What if another statement is later added to the switch ? 如果稍后将另一个语句添加到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. 我曾在各种情况下都需要break地方工作,包括默认情况。 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 . 我同意其他人的意见:如果default遵循最终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. 但我总是把它放进去因为我(或者更糟糕的是,其他一些可怜的野蛮人)可能会在默认的六个月之后无意中添加另一个case Sure, that would be an error, but only a typing error: why should I leave the code vulnerable to a hard-to-find bug 1 just because I made some dumbass fumble-fingered midnight mistake? 当然,这将是一个错误,但只是一个输入错误:为什么我应该让代码容易受到难以发现的错误1的影响 ,因为我犯了一些笨手笨脚的午夜错误? 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. 教师注意:在这种情况下插入break是一个防御性代码的例子,这总是一件好事。

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! 1这实际上发生在我所参与的一个项目中,它带走了我们[原文如此! 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 . default分支应该始终switch块中的最后一个case,所以无论有没有break ,都会在switch后继续执行。 So often break isn't used there, because it is superfluous. 所以经常在那里不使用break ,因为它是多余的。

However, if someone decides to change the order of case s, the existence or lack of break may make a huge difference. 但是,如果有人决定改变case的顺序,那么存在或不存在break可能会产生巨大的差异。 But then again, moving the default case in front of another one would be a serious mistake by itself. 但话说回来,将default案件移到另一个案件前面本身就是一个严重的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM