c++ - Why do semi-colons in c++ seem to implicitly handle errors? [tldr: they don't]
问题描述
This is in the visual studio 2019 community edition distribution of c++ on windows. I have this code below:
char c;
c = getc(stdin); // reading in some value
while(c != ';');
{
// do something
}
When the semi-colon was on the end of the while loop it stopped an exception from being thrown. One that seemed to be triggered in the loop, where 'do something' is. When I change it to this:
char c;
c = getc(stdin); // reading in some value
while(c != ';')
{
// do something -> exception thrown
}
Can anyone explain this to me? EDIT: Just to be more specific. In the '// do something' part of the loop I was changing the value of c and so forth, I didn't think it was relevant so I didn't include it.
解决方案
编码
while(c != ';');
{
// Do something
}
相当于
while(c != ';')
{
// Empty body
}
{
// Do something
}
所以你首先有一个(可能)无限循环,然后是它自己的嵌套范围内的“做某事”代码。
推荐阅读
- android - 在android中禁用屏幕录像机和屏幕短?
- image-processing - 使用单一模型的两级分类
- java - ARCore:检测到图像时如何在相框中播放视频
- javascript - JS:如果不附加事件监听器,这个函数如何被触发?
- reactjs - 如何在反应中将状态更新为函数
- qt - 如何更新 QML ListView 上 SingleTon 类型自定义 QAbstractListModel 的更改?
- java - 获取自定义 ArrayAdapter(Spinner) 中项目的位置
- c# - 无法访问映射上的变量(C#)
- elisp - 符号的函数定义为 void: after-load'
- javascript - 在 cypress 中使用机会插件