about the braces in case statement in switch

女生的网名这么多〃 提交于 2019-12-04 16:44:11

A switch statement is just a bunch of labels and a goto done by the compiler depending on the value of the thing inside the switch test.

When you have a local variable in a function, anywhere past the declaration of that variable you can use it. For instance:

int a;
// can use a now

However, in a switch statement, if you have a local variable:

case a:
    int a;
    break;
case b:
    // we can use a here because these cases are just labels used in a goto
    // i.e. the cases do *not* create a new scope

So when you have a variable in a case, the variable exists in cases below it but the variable won't exist because the code that initialized it got skipped by the case statement. It's hard for me to explain, maybe someone else can do a better job.

The braces fix this problem because they make the variable local, so that it doesn't exist in subsequent cases. It only gets created if that particular case gets entered, and if you forget a break and control falls through to the next case, the ending } ends the scope and causes the variable to be destroyed so it's not accessible from the next case, and the initialization can't be skipped.

So just remember that all the cases share scope. That might help you understand this.

After re-indenting your code and changing a few things so it compiles on my system, g++ compiles it without warnings.

My best guess is that it has something to do with the old rules for the scope of an object declared in a for loop (it used to live until the end of the enclosing scope; in modern C++ it's limited to the loop itself), but I can't quite be sure.

To help us figure this out, please indent the code properly, and show us the exact error message, including the line number. If the error says "line 42: ...", add a comment in your source like // this is line 42.

EDIT: Yes, I think that's the problem. In modern C++, your code is ok, because the loop variables are scoped to the loops themselves. Apparently Turbo implements a very old version of the language, so your variable a, for example, is visible all the way to the bottom of the switch statement. Enclosing each for loop in curly braces should avoid the warning:

{
    for (int a = 0; a <= 1; a++) {
        for (int b = 0; b <= 1; b++) {
            mat3[a][b] = mat1[a][b] + mat2[a][b];
        }
    }
}

EDIT2: Or better yet, stop using Turbo C++ and get a modern compiler.

EDIT3: And the reason that compiler warns about this is that even though it appears that i is always initialized before use, you could in principle refer to i in the case 2: section, bypassing the initialization. (Again, this only applies under the old rules.)

A cleaner way to work around this is probably to enclose each case section in braces:

switch (choice) {
    case 1: {
        ...
        break;
    }
    case 2: {
        ...
        break;
    }
}

(or, again, get a modern compiler, unless you have a really good reason to stick with Turbo C++).

A case block is not a new scope by itself. Any variable you declare within one is visible for the rest of the switch statement. But in the other case blocks, it's uninitialized. By adding the braces, you create a new scope so the other blocks can't see it.

For example:

switch (choice)
{
case 0:
    int a = 42;
    break;


case 1:
    std::cout << a << std::endl; // 'a' is uninitialized here
    break;
}


switch (choice)
{
case 0:
    {
        int a = 42;
        break;
    }


case 1:
    std::cout << a << std::endl; // error -- 'a' is not declared here
    break;
}

I don't see a situation like this in the code you posted, but that's what the error message means.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!