有没有一种对switch的break语句有更好方法的编程语言? [英] Is there a programming language with better approach for switch's break statements?

查看:48
本文介绍了有没有一种对switch的break语句有更好方法的编程语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在太多语言中,这是相同的语法:

It's the same syntax in a way too many languages:

switch (someValue) {

  case OPTION_ONE:
  case OPTION_LIKE_ONE:
  case OPTION_ONE_SIMILAR:
    doSomeStuff1();
    break; // EXIT the switch

  case OPTION_TWO_WITH_PRE_ACTION:
    doPreActionStuff2();
    // the default is to CONTINUE to next case

  case OPTION_TWO:
    doSomeStuff2();
    break; // EXIT the switch

  case OPTION_THREE:
    doSomeStuff3();
    break; // EXIT the switch

}

现在,您所知道的所有 break 语句都是必需的,因为 break 会在 break 语句丢失.我们以 OPTION_LIKE_ONE OPTION_ONE_SIMILAR OPTION_TWO_WITH_PRE_ACTION 为例.问题在于,我们仅需要这种跳到下一个情况"就可以了.非常非常非常罕见.通常,我们在 case 的末尾放置中断.

Now, all you know that break statements are required, because the switch will continue to the next case when break statement is missing. We have an example of that with OPTION_LIKE_ONE, OPTION_ONE_SIMILAR and OPTION_TWO_WITH_PRE_ACTION. The problem is that we only need this "skip to next case" very very very rarely. And very often we put break at the end of case.

初学者很容易忘记它.而且我的一位C老师甚至向我们解释了这似乎是C语言中的错误(不想谈论它:)

It's very easy for a beginner to forget about it. And one of my C teachers even explained it to us as if it was a bug in C language (don't want to talk about it :)

我想问一下是否有其他我不了解(或忘记)的语言来处理这种开关/情况:

I would like to ask if there are any other languages that I don't know of (or forgot about) that handle switch/case like this:

switch (someValue) {

  case OPTION_ONE:  continue; // CONTINUE to next case
  case OPTION_LIKE_ONE:  continue; // CONTINUE to next case
  case OPTION_ONE_SIMILAR:
    doSomeStuff1();
    // the default is to EXIT the switch

  case OPTION_TWO_WITH_PRE_ACTION:
    doPreActionStuff2();
    continue; // CONTINUE to next case

  case OPTION_TWO:
    doSomeStuff2();
    // the default is to EXIT the switch

  case OPTION_THREE:
    doSomeStuff3();
    // the default is to EXIT the switch

}

第二个问题:为什么我们在C语言中使用当前的中断方法有任何历史意义?也许继续使用下一个案例的频率要比我们最近使用的频率高得多?

The second question: is there any historical meaning to why we have the current break approach in C? Maybe continue to next case was used far more often than we use it these days ?

推荐答案

来自这篇文章,我可以列举一些不需要像 break 这样的语句的语言:

From this article, I can enumerate some languages that don't require a break-like statement:

  1. Ada(无失败)
  2. 埃菲尔(无失败)
  3. 帕斯卡(不掉线)
  4. 开始-失败
  5. Perl-继续
  6. Ruby(不会失败)
  7. VB,VBA,VBS,VB.NET(无故障)
  8. 要由其他人继续...

您的第二个问题很有趣.假设仅C,我相信,这一决定使该语言具有凝聚力.由于 break jump ,因此必须明确编写.

Your second question is pretty interesting. Assuming only C, I believe this decision keeps the language cohesive. Since break is a jump, it must be explicitly written.

这篇关于有没有一种对switch的break语句有更好方法的编程语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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