在 C/C++ 为什么 do while(表达式); 需要分号吗?
我的猜测是它只是使解析更容易,但我不明白为什么。
那么这有什么...
do
{
some stuff
}
while(test);
more stuff
这比...更好
do
{
some stuff
}
while(test)
more stuff
My guess is it just made parsing easier, but I can't see exactly why.
So what does this have ...
do
{
some stuff
}
while(test);
more stuff
that's better than ...
do
{
some stuff
}
while(test)
more stuff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
因为你正在结束声明。 语句以块(由大括号分隔)或分号结束。 “do this while this”是单个语句,不能以块结尾(因为它以“while”结尾),因此它需要一个分号,就像任何其他语句一样。
Because you're ending the statement. A statement ends either with a block (delimited by curly braces), or with a semicolon. "do this while this" is a single statement, and can't end with a block (because it ends with the "while"), so it needs a semicolon just like any other statement.
如果你看一下 C++ 语法,你会发现迭代语句定义为
请注意,只有
do-while
语句有 <代码>; 位于末尾。 所以,问题是为什么do-while
与其他的如此不同,以至于它需要额外的;
。让我们仔细看看:
for
和常规while
都以语句结尾。 但是do-while
以包含在()
中的控制表达式结束。 封闭()
的存在已经允许编译器明确地找到控制表达式的结尾:外部封闭)
指定表达式结束的位置,因此,整个do-while
语句结束。 换句话说,终止;
确实是多余的。然而,在实践中,这意味着,例如,以下代码
虽然从语法的角度来看有效,但实际上会被解析为:
这在形式上是合理的,但实际上并不直观。 我猜想,出于这样的原因,决定在 do-while 语句中添加一个更明确的终止符 - 分号。 当然,根据 @Joe White 的回答,还需要考虑简单一致性:C 中的所有普通(非复合)语句都以
;
结尾。If you take a look at C++ grammar, you'll see that the iteration statements are defined as
Note that only
do-while
statement has an;
at the end. So, the question is why thedo-while
is so different from the rest that it needs that extra;
.Let's take a closer look: both
for
and regularwhile
end with a statement. Butdo-while
ends with a controlling expression enclosed in()
. The presence of that enclosing()
already allows the compiler to unambiguously find the end of the controlling expression: the outer closing)
designates where the expression ends and, therefore, where the entiredo-while
statement ends. In other words, the terminating;
is indeed redundant.However, in practice that would mean that, for example, the following code
while valid from the grammar point of view, would really be parsed as
This is formally sound, but it is not really intuitive. I'd guess that for such reasons it was decided to add a more explicit terminator to the
do-while
statement - a semicolon. Of course, per @Joe White's answer there are also considerations of plain and simple consistency: all ordinary (non-compound) statements in C end with a;
.这是因为 while 语句在 do-while 循环内有效。
如果不需要分号,请考虑不同的行为:
It's because while statements are valid within a do-while loop.
Consider the different behaviors if the semicolon weren't required:
虽然我不知道答案,但一致性似乎是最好的论据。 为什么
要创建一个两者都没有的构造?
While I don't know the answer, consistency seems like the best argument. Every statement group in C/C++ is either terminated by
Why create a construct which does neither?
流程控制语句一致性
考虑一致性...
...所有这些流程控制结构都以分号结尾。
但是,与此相反,我们可以注意到,在块语句形式中,只有
do while
是分号分隔的:所以,我们有 ';' 或 '}',但绝不是“裸”')'。
语句分隔符的一致性
我们至少可以说,每个语句都必须用
;
或}
来分隔,这在视觉上可以帮助我们区分语句。如果不需要分号,请考虑:
很难在视觉上将其解析为不同的语句:
相比之下,以下内容更容易解析为
;
在while 条件告诉您向后查找
do
,并且下一个语句与该while
无关:考虑到人们缩进代码以实现流程,这是否重要可以理解吗? 是的,因为:
对预处理器使用的影响
还值得注意的是著名的预处理器 do-while 习惯用法:
这可以替换如下:
...yields...
如果分号不是 do 的一部分while 语句,那么
if
语句将被解释为:...并且,因为
if
后面有两个语句,所以它被认为是完整的,剩下else
语句不匹配。Flow control statement consistency
Considering consistency...
...all these flow-control constructs, end with a semicolon.
But, countering that we can note that of the block-statement forms, only
do while
is semicolon delimited:So, we have ';' or '}', but never a "bare" ')'.
Consistency of statement delimiters
We can at least say that every statement must be delimited by
;
or}
, and visually that helps us distinguish statements.If no semicolon were required, consider:
It's very difficult to visually resolve that to the distinct statements:
By way of contrast, the following is more easily resolved as a
;
immediately after awhile
condition tells you to seek backwards for thedo
, and that the next statement is unconnected to thatwhile
:Does it matter, given people indent their code to make the flow understandable? Yes, because:
Implications to preprocessor use
It's also worth noting the famous preprocessor do-while idiom:
This can be substituted as follows:
...yields...
If the semicolon wasn't part of the
do while
statement, then theif
statement would be interpreted as:...and, because there are two statements after the
if
, it's considered complete, which leaves theelse
statement unmatched.C 以分号结尾(而 Pascal 以分号分隔)。 在那里删除分号会不一致。
坦率地说,我讨厌在 do 循环中重用 while。 我认为重复直到不会那么混乱。 但是它就是这样啊。
C is semicolon-terminated (whereas Pascal is semicolon-separated). It would be inconsistent to drop the semicolon there.
I, frankly, hate the reuse of the while for the do loop. I think repeat-until would have been less confusing. But it is what it is.
在 C/C++ 中,空格对结构没有贡献(例如在 python 中)。 在 C/C++ 语句中必须以分号终止。 这是允许的:
In C/C++ whitespace don't contribute to structure (like e.g. in python). In C/C++ statements must be terminated with a semicolon. This is allowed:
我的答案是,当我们在
do.....while();
循环的终止中没有包含分号时,编译器可能会感到困惑。 如果没有这个,就不清楚:这就是为什么我们在 do......while 循环末尾添加分号,以指示如果条件为 false 则循环将在此处终止。
My answer is that, the compiler may get confusion, when we didn't include the semicolon in the termination of
do.....while();
loop. Without this it is not clear about:That's why we include semicolon in the end of
do......while
loop, to indicate the loop is terminating here if the condition is false.