English 中文(简体)
在开关案件中没有 b子?
原标题:The absence of brace syntax in switch cases?

我们都熟悉:

switch(x){
    case 1:
        do_something();
        break;
    case 2:
        do_something_else();
        break;
    default:
        do_default();
        break;
}

我很想知道,在任何一种语言中,是否存在着一种同义的改动:

switch(x){
    case(1){
        do_something();
    }
    case(2){
        do_something_else();
    }
    default{
        do_default();
    }
}

在我写这封信时,我认识到,没有clean的办法来表明某一案件是没有突破的,而方案流动应当排入以后的案件。 与此类似,但我总是很奇怪,为什么我从PHP那里看到的家庭语言(我来自PHP)在放弃<条码>switch建造<条码>时所看到的。

最佳回答

Not a C-like language, but Perl s given/when syntax actually looks like what you re describing. From the docs:

use feature ":5.10";
given($foo) {
    when (undef) {
        say  $foo is undefined ;
    }
    when ("foo") {
        say  $foo is the string "foo" ;
    }
    when ([1,3,5,7,9]) {
        say  $foo is an odd digit ;
        continue; # Fall through
    }
    when ($_ < 100) {
        say  $foo is numerically less than 100 ;
    }
    when (&complicated_check) {
        say  a complicated check for $foo is true ;
    }
    default {
        die q(I don t know what to do with $foo);
    }
}
问题回答

我不知道支持你所说明的第二种方法的任何语言,然而,基于C的语文支持类似的同义词,你可以在你的个案陈述中设立匿名密码组,例如:

switch(x){
  case(1): {
    do_something();
  } break;
  case(2): {
    do_something_else();
  } break;
  default: {
    do_default();
  }
}

This is good if you want to scope variables within your cases. Of course, you still need the break to avoid falling through.

在C中,所有图书馆的确界定了变量的范围(如果它们可以通过代码获取),但并未用于流动控制。 表明该案件倒闭的唯一清洁办法是评论该案件。

switch (a)
{
case 0:
    doSomethingSpecial();
    // Deliberate fall-through
case 1:
    doSomething();
    break;

case 2:
    doSomethingElse();
    break;
}

没有任何语言Im知道需要作为<代码>switch的一部分的图书馆。 这样做必然会产生许多意义,因为图书馆通常采用“锁定范围”,这是在<条码>>之间玩 play的常规

It may interest you to know, however, that in some languages (like C++) you can use braces as part of the case statement (rather than as part of the switch/case syntax itself) to introduce your own bounded scope:

switch (x) {
  case a:
    {
       ...
       break;
    }
}

有时,C++的汇编者将要求你这样做,最值得注意的是,当你在<条码>查询/编码说明中申报变量时。

http://en.wikipedia.org/wiki/ Conditionsal_%28programming%29#Case_and_switch_statements”rel=“nofollow”>wikipedia reference讨论转换/案件构造,紧随其后的是功能性方案拟订模式说明。

引文是,“父亲配对”可能被视为一种更先进的替代选择,可以替代假装和假装。

a 数学使用下列交换分机:

Switch[
  x,
  1,   action[1],
  2,   action[2],
  _,   defaultaction[]
]

这就是说“条码”(),然后是形式和行动顺序的改变。 标注的<代码>_为表示反对的一种模式,因此界定了违约行为。

在共同诉讼案的构造中(case,ccase, ecase),每种不同选择均采用单独单独排行的方式处理;无需“突破”和在贴标签之后,便有一个隐含的progn <>/code>,因此允许采用多种形式。

There s no fall-through option however...

(case x
    ((1 2 3)   (do-this))
    ((4 5)     (do-that))
    (otherwise (do-something-else)))




相关问题
XML-RPC Standard and XML Data Type

I was looking at XML-RPC for a project. And correct me if I m wrong, but it seems like XML-RPC has no XML datatype. Are you supposed to pass as a string? or something else? Am I missing something? ...

Is it exists any "rss hosting" with API for creating feeds

I am creating a desktop app that will create some reports. I want to export these reports as RSS or ATOM feeds. I can easily create feeds with Rome lib for Java. But I have no idea how to spread them. ...

Improving Q-Learning

I am currently using Q-Learning to try to teach a bot how to move in a room filled with walls/obstacles. It must start in any place in the room and get to the goal state(this might be, to the tile ...

High-traffic, Highly-secure web API, what language? [closed]

If you were planning on building a high-traffic, very secure site what language would you use? For example, if you were planning on say building an authorize.net-scale site, that had to handle tons ...

Def, Void, Function?

Recently, I ve been learning different programming langages, and come across many different names to initalize a function construct. For instance, ruby and python use the def keyword, and php and ...