Search code examples
c++clanguage-design

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


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

Solution

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