声明在switch语句中的变量 [英] Declaring variables inside a switch statement

查看:297
本文介绍了声明在switch语句中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到几个回答这个问题,我得到它 - 你不能声明并分配开关里面的变量。但我想,如果下面是正确的抛出一个错误说


  

错误:'诠释'

之前的预期前pression

code:

 开关(一){
    情况下0:
        INT J = 1;
        打破;
}

为什么把调用的NSLog()才导致没有错误?

 开关(一){
    情况下0:
        的NSLog(@跆拳道);
        INT J = 1;
        打破;
}


解决方案

您实际的如果您根据语言的语法做一个交换机内可以的声明变量。你得到,因为一个错误 0的情况:是一个标签,并在C中,它是非法的有声明作为后的第一个语句一个标签 - 请注意,编译器期望的前pression ,如方法调用,在正常分配等(奇异尽管它可能是,这就是规则。)

当你把的NSLog()首先,你避免了这种限制。您可以括在{}案件的内容牙套引入作用域块,也可以将开关之外将变量声明。你选择是个人的preference的问题。要知道,在} {声明的变量牙套仅仅是范围内有效,所以使用它也必须出现这些括号内的任何其他code。


编辑:

顺便说一句,这个怪癖并不像你想象的那样少见。在C和Java,这也是非法的使用局部变量声明为唯一的语句(意为没有用括号括起来)为 ,而循环,甚至可以在如果其他条款(事实上,这是覆盖在益智游戏#的\"Java谜题,我强烈建议)。我认为,我们一般不写这样的错误开始,因为这是毫无意义的声明一个变量作为唯一的语句这样的背景下,随着开关 / 情况结构,虽然有些人忽略了括号,因为在语句是控制流的关键语句。

要查看编译器扔配合,这种可怕的,毫无意义的片段复制到您的(Objective-)C code:

 如果(1)
    INT I;
其他
    INT I;
对于(INT答案= 1;答案< = 42;答案++)
    INT I;
而(1)
    INT I;

    INT I;
而(1);

另一个原因始终使用{}括号来界定这种结构的主体。 : - )

I saw a few answers to this issue, and I get it — you can't declare and assign variables inside a switch. But I'm wondering if the following is correct at throwing an error saying

error: expected expression before 'int'

Code:

switch (i) {
    case 0:
        int j = 1;
        break;
}

Why would putting a call to NSLog() before it result in no errors?

switch (i) {
    case 0:
        NSLog(@"wtf");
        int j = 1;
        break;
}

解决方案

You actually can declare variables within a switch if you do it according to the syntax of the language. You're getting an error because "case 0:" is a label, and in C it's illegal to have a declaration as the first statement after a label — note that the compiler expects an expression, such as a method call, normal assignment, etc. (Bizarre though it may be, that's the rule.)

When you put the NSLog() first, you avoided this limitation. You can enclose the contents of a case in { } braces to introduce a scoping block, or you can move the variable declaration outside the switch. Which you choose is a matter of personal preference. Just be aware that a variable declared in { } braces is only valid within that scope, so any other code that uses it must also appear within those braces.


Edit:

By the way, this quirk isn't as uncommon as you might think. In C and Java, it's also illegal to use a local variable declaration as the lone statement (meaning "not surrounded by braces) in a for, while, or do loop, or even in if and else clauses. (In fact, this is covered in puzzler #55 of "Java Puzzlers", which I highly recommend.) I think we generally don't write such errors to begin with because it makes little sense to declare a variable as the only statement in such contexts. With switch / case constructs, though, some people omit the braces since the break statement is the critical statement for control flow.

To see the compiler throw fits, copy this horrific, pointless snippet into your (Objective-)C code:

if (1)
    int i;
else
    int i;
for (int answer = 1; answer <= 42; answer ++)
    int i;
while (1)
    int i;
do
    int i;
while (1);

Yet another reason to always use { } braces to delimit the body of such constructs. :-)

这篇关于声明在switch语句中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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