关于"switch"中"case"语句中的花括号 [英] About the braces in a 'case' statement in 'switch'

查看:124
本文介绍了关于"switch"中"case"语句中的花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,当我试图编写代码以仅对两个2 * 2矩阵进行加法和减法时,我使用了 switch 语句,但出现了一个错误:

Today, while I was trying to write code to just add and subtract the two 2*2 matrices, in which I used a switch statement, I got an error:

函数main()中局部变量的案例旁路初始化

case bypass initialization of local variable in function main()

代码

#include <iostream.h>
#include <conio.h>
#include <string.h>

int
main()
{
    int mat1[2][2], mat2[2][2], mat3[2][2];

    cout << "Enter the elements in the first matrix";
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            cin >> mat1[i][j];
        }
    }

    cout << "\n\nEnter the elements of the second matrix";

    for (int k = 0; k < 2; k++) {
        for (int l = 0; l < 2; l++) {
            cin >> mat2[k][l];
        }
    }

    cout << "\n\nsaved......";

    int choice;
    cout << "\n\n\nFor adding these two matrices,press 1";
    cout << "\nFor subtracting these two matrices,press 2";
    cin >> choice;

    switch (choice) {
    case 1:

        cout << "The addition of the two matrices will yield";
        for (int a = 0; a <= 1; a++) {
            for (int b = 0; b <= 1; b++) {
                mat3[a][b] = mat1[a][b] + mat2[a][b];
            }
        }
        break;

    case 2:
        cout << "The subtraction of the two matrices will yield";
        for (int c = 0; c <= 1; c++) {
            for (int d = 0; d <= 1; d++) {
                mat3[c][d] = mat1[c][d] - mat2[c][d];
            }
        }
        break;
    }
    getch();
    return 0;
}

我还发现我可以通过将案例代码放到大括号中来消除这种错误,

I also found that I can take the rid of this error by placing the code of case(s), into braces, NOW,

  1. 我的困惑是关于错误 ...
  2. &大括号在 case ....
  3. 中的要求
  1. my confusion is about the error...
  2. & the requirement of braces in case....

(我知道我没有使用新的编码约定,例如< iostream> ,std名称空间等,因为我已经在

(I know I haven't used the new coding conventions, like <iostream>, std namespace, etc., etc. as I have written it in the Turbo C++ compiler, so a to-the-point answer is humbly requested.)

推荐答案

switch语句只是一堆标签和一个 goto ,由编译器根据<代码>切换测试.

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

但是,在 switch 语句中,如果您有局部变量:

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

因此,当您在 case 中有一个变量时,该变量在它下面的 case s中存在,但是该变量将不存在,因为初始化它的代码已被跳过通过案例陈述.我很难解释,也许其他人可以做得更好.

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.

大括号解决了此问题,因为它们将变量设置为局部变量,因此在随后的 case 中不存在该变量.仅当输入特定的 case 时才创建它,并且如果您忘记了 break 并且控制权进入下一个 case ,则结尾的} 终止作用域,并导致变量被破坏,因此无法在下一个 case 中访问它,并且无法跳过初始化.

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.

因此,请记住所有 case 的共享范围.这可能有助于您理解这一点.

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

这篇关于关于"switch"中"case"语句中的花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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