关于switch中case语句的大括号 [英] about the braces in case statement in switch
问题描述
今天,当我尝试编写代码以仅添加&减去两个2 * 2矩阵,在其中我使用了switch语句,我得到了一个错误在函数main()中局部变量的案例旁路初始化
today while i was trying to write a code to just add & subtract the two 2*2 matrices, in which i used switch statement, i got an error 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;
}
我还发现我可以通过将case的代码放到花括号中来消除这种错误,
I also found that I can take the rid of this error by placing the code of case(s) ,into braces, NOW,
- 我对
error
... 感到困惑
- &
case
.... 中大括号的要求
- my confusion is about the
error
... - & the requirement of braces in
case
....
(我知道我没有使用新的编码约定,例如<iostream>
,std名称空间等,等等,因为我已经在turbo c ++编译器中编写了它,所以很谦逊地要求回答)
(I know I haven't used the new coding conventions, like <iostream>
, std namespace etc. etc. as I have written it in turbo c++ compiler, so a to the point answer is humbly requested)
推荐答案
switch语句只是一堆标签,而编译器根据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
中,但该变量将不存在,因为初始化该变量的代码被case语句跳过了.我很难解释,也许其他人可以做得更好.
So when you have a variable in a case
, the variable exists in case
s 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 case
s. 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 case
s share scope. That might help you understand this.
这篇关于关于switch中case语句的大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!