Why do some languages need semicolons?

2019-02-11 23:56发布

I understand that semicolons indicate the end of a line in languages like Java, but why?

I get asked this a lot by other people, and I can't really think of a good way to explain how it works better than just using line breaks or white space.

7条回答
Deceive 欺骗
2楼-- · 2019-02-12 01:01

Short answer:

Because everyone else does it.

Not, nor everyone. Furthermore, many popular languages like Python, Ruby, or Visual Basic, don't use semicolon as end of statement but line breaks. Many, not "everyone", still uses semicolon because historical reasons, not rational argumentation: semicolons had a important role to replace the punched-card format in first age of computation, but today it can be totally discarded.

In fact, there're two popular ways of specify an end of statement:

  1. Using a semicolon.
  2. Leaving as is. This makes the compiler read a line break as end of statement. When you want extend your statement to more of one line, you simply use a special character (like \ in Python) to say that the statement has not finished.

In order to make a code more readable, using a special character to specify an end of statement should be an exception, not the rule.

查看更多
登录 后发表回答