c++ - 为什么 C++ 中的分号似乎隐式处理错误? [tldr : they don't]

标签 c++ visual-studio exception visual-c++ while-loop

这是在 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/

相关文章:

c++ - 使用 Cron 运行 C++ 程序

C++ std::random 和枚举类

c++ - 根据模板类型在 std::to_string() 和 .toString() 之间切换

visual-studio - 如何配置 VS2008 以在具有多个项目的解决方案中仅打开一个 Web 服务器?

android - Visual Studio/Xamarin OnClickListener

visual-studio - Visual Studio 云资源管理器失败 : Unable to retrieve subscription

java - 如何在 Java 的 MVC 设计中创建 View 部分和模型部分之间的关​​系?

c# - 如何区分 BackgroundWorker.RunWorkerCompleted 事件处理程序中的不同异常类型

c++ - MFC:拖动滚动条拇指时的奇怪行为

java - Java 中的检查异常和未检查异常如何工作?