views:

1018

answers:

6

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
+25  A: 

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.

Joe White
Well, there is some legitimacy to the question.for(;;) {do something; }Doesn't actually end with any semicolons other than the ones that go with the contained statements.
Matthias Wandel
@Matthias: but it ends with a curly brace.
kitchen
And the reason that it has to be a "statement" is because a computer is putting your code together for you into a program.. the consistency isn't for the programmer's sake
ryansstack
It still seems arbitary to me whether you define the iteration statement asDO statement WHILE '(' expression ')' ';'DO statement WHILE '(' expression ')'but I suppose it is more conistent. It's basically saying you must end this with a statement but it has to be a semi colon.
justinhj
If it seems arbitory try writing the grammer without it. Since Statements can be nested you need to define a delimiter. In C/C++ this is the ';'.
Martin York
This is right answer.
Stuart
This answer is a mere assertion ("a stetement either ends with a block or with a semicolon"), buy it doesn't provide any reasons as to why it must be so. So it doesn't really answer anything. It is obvious to anyone that `do-while` construct has a very special two-part structure, not present in other language constructs. This particular detail actally makes it possible to properly detect the end of `do-while` statement even without a semicolon. So, the above claim that it "must" end with a semicolon is plain incorrect. No, there's nothing there that would make it a "must".
AndreyT
The decision to require a semicolon was made to avoid some counter-intuitive parsings in situations when user forgets to properly enclose the controlling expression in braces `()` (see my response for details).
AndreyT
@Martin Yes I understand the need for a delimiter between statements in general, but my question is why you need it in this case. This answer merely states that you need it by definition. That's not really what I was looking for. The accepted answer gives a clear example of how you can not write a valid statement if the grammar were different.
justinhj
The accepted answer just makes a different assertion, which you intuitively accepted as "correct" and as a "clear example", while in fact it is absolutely incorrect. The problem you believe would arise "if the grammar were different", wouldn't actually arise.
AndreyT
+7  A: 

While I don't know the answer, consistency seems like the best argument. Every statement group in C/C++ is either terminated by

  1. A semicolon
  2. A brace

Why create a construct which does neither?

JaredPar
So, why can't I do this:int testFunc(int x) return x+1;
Rocketmagnet
@Rocketmagnet, you can't allow all statements (including null statement), because "void f() ;" (function definition with a "null statement" as body) would not be different from "void f();" (function declaration only). Both look the same. Requiring a compound statement is a good thing. And anyway, a function definition isn't a statement.
Johannes Schaub - litb
+1  A: 

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);
lothar
+2  A: 

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.

Nosredna
+24  A: 

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--);
Don
Nice example! .
Andrew Coleson
Thanks, that seems to explain the apparent arbitrary ;
justinhj
The fact that there is no instruction between do and while would be enough for a compiler to find the difference between while "end of do" and while "new loop". Your example is interesting but i don't think it explains why there is a semicolon after do's while.
Benoît
@Benoit: not so, because an empty do/while loop and an empty while loop are both legal. If it were also legal to omit the semi-colon following do/while, then there is nothing to distinguish Don's example from "do while(x > 0); x--; while(x = y--);" Which loops forever. To avoid ambiguity, at least one of those empty loops would have to be banned.
Steve Jessop
onebyone: Not true. `do while(...);` isn't legal. An empty `do-while` loop is written like: `do ; while(...);` or `do {} while(...);` It wouldn't compile otherwise.
Mehrdad Afshari
-1. The example seems to imply that without trailing `;` requirement the code would be misinterpreted somehow. In reality, the language grammar (even in its current form) does not allow any alleged misinterpretations of the above code, even if the trailing `;` requirement is removed. Benoît is right: the example does not explain anything relevant to the question. This answer is incorrect. I allow a possibility that Don implies something that I'm missing, but until he clarifies what he meant this is a solid -1.
AndreyT
Additionally, note that C language grammar is so specifically designed that the syntactical analyser doesn't have to perform any significant look-ahead in order to recognize the current syntactical construct. This answer implies that in order to allow the proper recognition of the statement *in the body* of `do-while` the grammar needs a `;` *at the end* of `do-while`. This just doesn't make any sense. The C grammar, once again, is specifically designed to be free of such look-ahead requirements.
AndreyT
+2  A: 
AndreyT
You can see that the question hasn't been edited, the number of edits is shown when there are any. The question seems straightforward enough that I hope it means what I intend. The accepted answer seems superior to yours, since it shows that without the semi-colon, valid uses of the grammar become ambiguous. In your example it is merely confusing to the user.
justinhj
@justinhj: No, absolutely incorrect. The accepted answer is confusing, since it implies some "different behaviors", but fails to explain what behaviors these would be. Expectedly, as you can easily see by yourself, one of the commenters on the accepted answer got actually confused when he assumed that without semicolon requirement the inner `while` statement would get mis-associated with the outer `do`. That's simply not true. The grammar is specifically crafted not to let this happen, semicolon or not.
AndreyT
I wish the author of the accepted answer would clarify, which "different behaviors" he was implying... Without that the accepted answer is not an answer at all.
AndreyT
The different behaviour is quite clear to me. Without the semi-colon requirement you simply cannot nest a while loop inside a do-while, and this seems wrong.
justinhj
@justinhj: But that's not true. Someone told you so and you just accepted it rigth away without a single doubt. In fact, even without the semi-colon requirement (after `do-while`) you still can nest a `while` loop inside a `do-while` one and it still will be correctly associated. There's no problem in writing the language grammar that way, and in fact it is already written that way.
AndreyT
@justinhj: Also, note the comments I just added after the "accepted" aswer where I explain in more detail why it is incorrect.
AndreyT
@AndreyT "Someone told you so and you just accepted it rigth away without a single doubt." That is not the case, I spent some time satisfying myself that this is true.
justinhj