为什么switch语句设计需要休息? [英] Why was the switch statement designed to need a break?

查看:166
本文介绍了为什么switch语句设计需要休息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个简单的switch语句

Given a simple switch statement

switch (int)
{
    case 1 :
    {
        printf("1\n");
        break;
    }

    case 2 : 
    {
        printf("2\n");
    }

    case 3 : 
    {
        printf("3\n");
    }
}

缺乏的情况下,2 break语句,意味着将继续执行在code里面的情况下3。
这不是一个意外;它的目的是这样的。为什么这个决定做?这是否与提供具有块自动断语义有什么好处?是什么道理呢?

The absence of a break statement in case 2, implies that execution will continue inside the code for case 3. This is not an accident; it was designed that way. Why was this decisions made? What benefit does this provide vs. having an automatic break semantic for the blocks? What was the rationale?

推荐答案

许多答案似乎把重点放在告吹作为的理由的要求该破<能力/ code>语句。

Many answers seem to focus on the ability to fall through as the reason for requiring the break statement.

我相信这只是一个错误,主要原因是因为C在设计时并没有用这些构建将如何使用几乎一样多的经验。

I believe it was simply a mistake, due largely because when C was designed there was not nearly as much experience with how these constructs would be used.

彼得·范·德·林登使得案件在他的著作C专家编程:

Peter Van der Linden makes the case in his book "Expert C Programming":

我们分析Sun C编译器源
  看多久默认秋天
  通过使用。太阳ANSI C
  编译器的前端有244开关
  语句,其中每一个具有一个
  平均七个案件。到秋季
  发生在所有这些情况下仅为3%。

We analyzed the Sun C compiler sources to see how often the default fall through was used. The Sun ANSI C compiler front end has 244 switch statements, each of which has an average of seven cases. Fall through occurs in just 3% of all these cases.

在换句话说,正常开关
  行为的错误的97%的时间。
  这不只是一个编译器 - 上
  相反,通过在那里使用秋天
  在该分析中是经常用于
  更经常发生的情况
  在编译器比其他软件,
  例如,编译时,运营商
  可以有一个或两个
  操作数:

In other words, the normal switch behavior is wrong 97% of the time. It's not just in a compiler - on the contrary, where fall through was used in this analysis it was often for situations that occur more frequently in a compiler than in other software, for instance, when compiling operators that can have either one or two operands:

switch (operator->num_of_operands) {
    case 2: process_operand( operator->operand_2);
              /* FALLTHRU */

    case 1: process_operand( operator->operand_1);
    break;
}


  
  

案例秋天通过如此广泛
  公认的缺陷,即有
  即使是一个特殊的注释约定,
  如上图所示,告诉皮棉这是
  的情况下,这些3%实际上是一个
  砸锅被期望的。

Case fall through is so widely recognized as a defect that there's even a special comment convention, shown above, that tells lint "this is really one of those 3% of cases where fall through was desired."

我认为这是一个好主意,C#,要求在每一种情况下块的末尾明确的跳转语句(同时仍允许多个案例标签堆叠 - 只要有只陈述单块)。在C#中,你仍然可以有一个案例秋天通过另一个 - 你只需要通过使用跳到下一个case,使秋季直通明确一个转到

I think it was a good idea for C# to require an explicit jump statement at the end of each case block (while still allowing multiple case labels to be stacked - as long as there's only a single block of statements). In C# you can still have one case fall through to another - you just have to make the fall thru explicit by jumping to the next case using a goto.

这太糟糕的Java并没有趁机从C语义打破。

It's too bad Java didn't take the opportunity to break from the C semantics.

这篇关于为什么switch语句设计需要休息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆