In C/C++ why does the do while(expression); need a

2020-01-30 07:26发布

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

8条回答
家丑人穷心不美
2楼-- · 2020-01-30 07:50

It's because while statements are valid within a do-while loop.

Consider the different behaviors if the semicolon weren't required:

int x = 10;
int y = 10;

do 
  while(x > 0)
    x--;
while(x = y--);
查看更多
Melony?
3楼-- · 2020-01-30 07:54

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
{
  some stuff; more stuff; even more stuff;
}
while(test);
查看更多
一夜七次
4楼-- · 2020-01-30 07:57

Flow control statement consistency

Considering consistency...

if (expr) statement;
do statement; while (expr);
for (expr; expr; expr) statement;
while (expr) statement;

...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:

if (expr) { ... }
do { ... } while (expr);
for (expr; expr; expr) { }
while (expr) { }

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:

do statement1; while (expr1) statement2; do ; while (expr2) statement3; while (expr3) statement4;

It's very difficult to visually resolve that to the distinct statements:

do statement1; while (expr1)
statement2;
do ; while (expr2)
statement3;
while (expr3) statement4;

By way of contrast, the following is more easily resolved as a ; immediately after a while condition tells you to seek backwards for the do, and that the next statement is unconnected to that while:

do statement1; while (expr1); statement2; do ; while (expr2); statement3; while (expr3) statement4;

Does it matter, given people indent their code to make the flow understandable? Yes, because:

  • people sometimes make mistakes (or have them transiently as the code's massaged) and if it visually stands out that means it will be fixed more easily, and
  • macro substitutions can throw together lots of statements on one line, and we occasionally need to visually verify the preprocessor output while troubleshooting or doing QA.

Implications to preprocessor use

It's also worth noting the famous preprocessor do-while idiom:

#define F(X) do { fn(X); } while (false)

This can be substituted as follows:

if (expr)
    F(x);
else
    x = 10;

...yields...

if (expr)
    do ( fn(x); } while (false);
else
    x = 10;

If the semicolon wasn't part of the do while statement, then the if statement would be interpreted as:

if (expr)
    do-while-statement
; // empty statement
else
    x = 10;

...and, because there are two statements after the if, it's considered complete, which leaves the else statement unmatched.

查看更多
ゆ 、 Hurt°
5楼-- · 2020-01-30 07:58

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.

查看更多
相关推荐>>
6楼-- · 2020-01-30 07:58

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:

  1. when the do ends?
  2. If the while may a separate loop followed immediately after do loop.

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.

查看更多
smile是对你的礼貌
7楼-- · 2020-01-30 08:02

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.

查看更多
登录 后发表回答