我可以把code例之外的交换机? [英] Can I put code outside of cases in a switch?

查看:67
本文介绍了我可以把code例之外的交换机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设:

switch ( test ) {

  // Set some variables, call some functions ?
  int x = 1 ;
  int y = function(x) ;
  //

  case 1 : 
    // Process for test = 1
    ...
    break;

  case 5 : 
    // Process for test = 5
    ...
    break;

  default : 
    // Process for all other cases.
    ...

}

是不是法律来执行code的额外位我的第一个情况前加入?我从来没有见过这样的例子。

Is it 'legal' to execute that extra bit of code I added before the first case? I've never seen this in examples.

推荐答案

一是如何交换机上的一些背景(真)的作品:

First some background on how switch (really) works:

A 开关通常被看作是选择一张code的依赖于一些前pression的值来执行,如在结构

A switch is usually thought of as a construct that selects a piece of code to execute depending on the value of some expression, as in

switch (x) {
case 1:
    foo();
    break;

case 2:
    bar();
    break;
}

然而,这是更准确的认为开关作为计算转到语句的形式。例如,以下是完全合法的:

However, it's more accurate to think of a switch as a form of computed goto statement. For example, the following is perfectly legal:

switch (x) {
    puts("I can't be reached");
case 1:
    if (cond) {
case 2:
        puts("Either x == 1 && cond, or x == 2");
    }
}

根据值 X ,程序将跳转为案例1 案例2 (或过去开关如果 X 既不是1也不是2)。

Depending on the value of x, the program will jump to either case 1 or case 2 (or past the switch if x is neither 1 nor 2).

您的程序将编译为C(与 X 开关,因为初始化被跳过),而不是像C ++。原因是,C ++不允许跳转到一个情况标签跨越一个变量的初始化。对于简单的类型,如 INT ,跳过 INT X; 是允许的(因为没有初始化参与),而不是跳过在 INT X = 1;

Your program will compile as C (with junk values for x and y inside the switch, since the initializations are skipped), but not as C++. The reason is that C++ does not allow a jump to a case label to cross the initialization of a variable. For simple types like int, skipping over int x; is allowed (since no initialization is involved), but not skipping over int x = 1;.

造成这种差别的主要动机是可能是让跳转到一个情况标签交叉使用C初始化时++构造参与将是不安全的。例如,如果C ++允许一个情况标签的定义之后发生 My_class my_object 某些范围内,则跳转到情况标签将跳过 my_object 的构造函数,但在退出作用域时仍然运行其析构函数。

The main motivation for this difference is probably that letting a jump to a case label cross an initialization in C++ would be unsafe when constructors are involved. For example, if C++ allowed a case label to occur after a definition My_class my_object within some scope, then jumping to that case label would skip my_object's constructor but still run its destructor when exiting the scope.

同样的限制适用于转到在C ++中。你不能用它跳进块,过去变量的初始化。

The same restrictions apply to goto in C++. You can't use it to jump into a block and past a variable initialization.

作为一个方面说明,开关遵循相同的一般语法为如果。如果作为C11标准(ISO / IEC 9899:2011,第6.8.4节)给出的的语法为

As a side note, switch follows the same general syntax as if and while. The syntax of if as given in the C11 standard (ISO/IEC 9899:2011, section 6.8.4) is

如果(例如pression)语句

if ( expression ) statement

,而开关的语法是

开关(例如pression)语句

switch ( expression ) statement

据的语句的来讲唯一的区别(在C - C ++增加了上述一些限制)是它允许包含情况标签(和)的开关而不是一个如果(除非如果 A 开关内发生)。

The only difference as far as statement is concerned (in C -- C++ adds some more limitations as mentioned above) is that it is allowed to contain case labels (and break) for a switch but not for an if (unless the if occurs within a switch).

就像用如果,你甚至可以离开花括号,写code如下所示。 (这是否是不必要的混乱是另一个讨论。)

Just as with an if, you can even leave off the braces and write code like the following. (Whether this is unnecessarily confusing is another discussion.)

switch (x) case 1: case 2: puts("x is 1 or 2");

在语法上,情况默认标签属于同一类别为转到标签。在C11标准的第6.8.1节定义如下:

Syntactically, case and default labels belong in the same category as goto labels. Section 6.8.1 of the C11 standard has the following definition:

标记的语句:的结果
          的标识 语句的结果
           情况 恒前pression 语句的结果
           默认 语句

labeled-statement:
        identifier : statement
        case constant-expression : statement
        default : statement

这篇关于我可以把code例之外的交换机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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