这是在 Windows 上的 c++ 的 visual studio 2019 社区版分发中。我在下面有这段代码:
char c;
c = getc(stdin); // reading in some value
while(c != ';');
{
// do something
}
当分号位于 while 循环的末尾时,它会阻止抛出异常。一个似乎在循环中被触发,“做某事”所在的地方。当我将其更改为:
char c;
c = getc(stdin); // reading in some value
while(c != ';')
{
// do something -> exception thrown
}
谁能给我解释一下? 编辑:只是为了更具体。在循环的“//do something”部分,我改变了 c 的值等等,我认为它不相关,所以我没有包括它。
最佳答案
代码
while(c != ';');
{
// Do something
}
相当于
while(c != ';')
{
// Empty body
}
{
// Do something
}
所以您首先有一个(可能)无限循环,然后在它自己的嵌套范围内的“做某事”代码。
关于c++ - 为什么 C++ 中的分号似乎隐式处理错误? [tldr : they don't],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58606688/