关于“case”中的大括号; “switch”中的语句
今天,当我尝试编写代码来对两个 2*2 矩阵进行加减运算时,我在其中使用了 switch 语句,但出现了错误:
案例绕过函数main()中局部变量的初始化
case 绕过函数 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 代码放入大括号中来消除此错误,现在,
- 我的困惑是关于
错误...
- &
case
中大括号的要求......
(我知道我还没有使用新的编码约定,例如
、std 命名空间等正如我在 Turbo C++ 编译器中编写的那样,因此点答案是虚心请教。)
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:
case bypass initialization of local variable in function main()
Code
#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,
- my confusion is about the
error
... - & the requirement of braces in
case
....
(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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
switch 语句只是一堆标签和编译器根据
switch
测试中内容的值完成的goto
。当函数中有局部变量时,在该变量声明之后的任何位置都可以使用它。例如:
但是,在
switch
语句中,如果您有一个局部变量:因此,当您在
case
中有一个变量时,该变量存在于case< /code> 在它下面,但是该变量将不存在,因为初始化它的代码被 case 语句跳过了。我很难解释,也许其他人可以做得更好。
大括号解决了这个问题,因为它们使变量成为本地变量,因此它不会存在于后续的
case
中。仅当输入特定的case
时才会创建它,并且如果您忘记了break
并且控制权会转到下一个case
,则结束 < code>} 结束作用域并导致变量被销毁,因此无法从下一个case
访问它,并且无法跳过初始化。因此,请记住所有
case
共享范围。这可能会帮助你理解这一点。A switch statement is just a bunch of labels and a
goto
done by the compiler depending on the value of the thing inside theswitch
test.When you have a local variable in a function, anywhere past the declaration of that variable you can use it. For instance:
However, in a
switch
statement, if you have a local variable:So when you have a variable in a
case
, the variable exists incase
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.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 particularcase
gets entered, and if you forget abreak
and control falls through to the nextcase
, the ending}
ends the scope and causes the variable to be destroyed so it's not accessible from the nextcase
, and the initialization can't be skipped.So just remember that all the
case
s share scope. That might help you understand this.重新缩进代码并更改一些内容以便它可以在我的系统上编译后,g++ 会在没有警告的情况下编译它。
我最好的猜测是,它与 for 循环中声明的对象范围的旧规则有关(它过去一直存在到封闭范围的末尾;在现代 C++ 中,它仅限于循环本身),但是我不太确定。
为了帮助我们解决这个问题,请正确缩进代码,并向我们显示确切的错误消息,包括行号。如果错误显示“line 42: ...”,请在源代码中添加注释,例如
// this is line 42
。编辑:是的,我认为这就是问题所在。在现代 C++ 中,您的代码没问题,因为循环变量的作用域为循环本身。显然,Turbo 实现了该语言的一个非常旧的版本,因此您的变量
a
一直可见到switch
语句的底部。将每个for
循环括在大括号中应该避免警告:EDIT2:或者更好的是,停止使用 Turbo C++ 并获取现代编译器。
EDIT3:编译器对此发出警告的原因是,即使看起来
i
总是在使用前初始化,但原则上您可以在中引用
部分,绕过初始化。 (同样,这仅适用于旧规则。)i
case 2:解决此问题的一种更简洁的方法可能是将每个
case
部分括在大括号中:(或者,再次,获取一个现代编译器,除非您有一个坚持使用 Turbo C++ 确实是一个很好的理由)。
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 theswitch
statement. Enclosing eachfor
loop in curly braces should avoid the warning: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 toi
in thecase 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:(or, again, get a modern compiler, unless you have a really good reason to stick with Turbo C++).
case
块本身并不是一个新作用域。您在其中声明的任何变量对于switch
语句的其余部分都是可见的。但在其他case
块中,它未初始化。通过添加大括号,您可以创建一个新的范围,这样其他块就看不到它。例如:
我在您发布的代码中没有看到这样的情况,但这就是错误消息的含义。
A
case
block is not a new scope by itself. Any variable you declare within one is visible for the rest of theswitch
statement. But in the othercase
blocks, it's uninitialized. By adding the braces, you create a new scope so the other blocks can't see it.For example:
I don't see a situation like this in the code you posted, but that's what the error message means.