开发者

Why does "do while" require a semicolon at the end? (C/C++/Java/etc) [duplicate]

开发者 https://www.devze.com 2023-02-24 23:21 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: In C/C++ why does the do 开发者_如何学Gowhile(expression); need a semi colon?
This question already has answers here: Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消