Possible Duplicate:
In C/C++ why does the do 开发者_如何学Gowhile(expression); need a semi colon?
I understand the structure of a "do while" loop compared to a "while" loop. What I don't understand, is why does the language require this syntax:
do{ statements(); } while(condition);
Is it absolutely necessary for the language to have a semicolon at then end of this expression? Or is this more for ease of writing a compiler?
Since the }
isn't the end, then it helps to have some way to know that the end of the statement is reached, hence the semi-colon.
It is possible to write a language without it, but it makes more sense to require it, in these languages.
At least in java, you can't write:
} while (i < 10) && (j != 8);
without getting such errormessages:
NodeSorter.java:24: ';' expected
} while (i < 10) && (j != 8);
^
NodeSorter.java:24: not a statement
} while (i < 10) && (j != 8);
^
NodeSorter.java:24: ';' expected
} while (i < 10) && (j != 8);
^
3 errors
So I guess it is for reasons of symetry - maybe it is more easy to write a parser that way.
精彩评论