Take the 2-minute tour ×

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.

share|improve this question
Duplicate : stackoverflow.com/questions/10254588/… –  fastcodejava Mar 13 '13 at 0:06

5 Answers 5

They don't signal end of line, they signal end of statement.

There are some languages that don't require them, but those languages don't allow multiple statements on a single line or a single statement to span multipile lines (without some other signal like VB's _ signal).

Why do some languages allow multiple statements on a line? The philosophy is that whitespace is irrelevant (an end of line character is whitespace). This allows flexibility in how the code is formatted as formatting is not part of the semantic meaning.

share|improve this answer
Ah, yes. I do know some languages that still allow multiple statements on one line, but by separating with a special character: statement1 | statement2 | statement 3, and I know some that allow one statement across multiple lines: statement 1/3 + statement 2/3 + statement 3/3 (comments don't allow multiple lines apparently, so just imagine this was 3 separate lines!) –  BlueThen Jan 15 '11 at 18:46
Well, you could make the semicolon optional and use it if you want to have multiple statements in one line. This is what JavaScript does. –  Tim Büthe Mar 26 '13 at 10:58

Many languages allow you to put as much spacing as you like. This allows you to be have control over how the code looks.


 String result = "asdfsasdfs"
               + "asdfs"
               + "asdfsdf";

Because you are allowed to insert extra newlines you can split that line across several lines without problem. The language still needs to know the line is finished that is why you need a semicolon.

share|improve this answer
JavaScript allows this with optional semicolons, so they are not needed per se –  Tim Büthe Mar 26 '13 at 11:00
If you put the + sign at the end of each line then the syntax could parsed easily without the use of semicolon. –  Bernard Feb 27 at 19:20

Short answer:

Because everyone else does it.

In theory a language's statement is whatever the language designer is able to syntactically interpret when they parse your file. So if the language designer did not want to have semicolons they could have periods, dashes, spaces, newlines, or whatever to denote the separation of a statement.

Language designers often make the syntax easy to understand so that it can become popular.

Wikipedia: Semicolon Usage in Computer Languages

So if some language designer created a language that used ':-)' to denote the end of a statement it would, 1) be hard to read; 2) not be popular with people who already are used to using a ';'.

echo "Take Care" :-)

share|improve this answer
I see how it'd be pretty to a typical programmer, but I feel like that's only because they're used to this design. Why would the very first programming language to use semicolons, use it? Is it easier for the compiler? –  BlueThen Jan 15 '11 at 18:52
Partially that, but JavaScript has optional semis which are nice because you CAN separate multiple statements one line if you want to. But it also produces some gotchas where not using one can lead to confusion with popular JS patterns like using parens around functions to evaluate and fire them immediately after definition. I actually enjoy white-space end-of-statements but it's never bothered me in JS where I tend to write after every line just to be explicit and avoid confusion for the next dev. It's an extra character here and there but we format in whatever fashion we like. That fits JS. –  Erik Reppen Jan 8 '13 at 0:24

The languages do it, as it signifies the end of a statement, not an end of the line, which means that you can compress code, to make it smaller and take up less space.

Take the C++ code (#include <iostream>):

    std::cout << "did you know?" << std::endl; 
    std::cout << "; signifies **end of statement**" << std::endl;
    std::cout << "**not the end of the line**" << std::endl;

It could also be written

for(int i = 0; i < 5; ++i){std::cout << "did you know?" << std::endl; std::cout << "; signifies **end of statement**" << std::endl; std::cout << "**not the end of the line**" << std::endl;}
share|improve this answer

Some programming languages use it to signify the end of a statement thus making the language oblivious to white-space from a statement standpoint. One thing to bear in mid is that if at compile time you are checking for either a new line or a semicolon and then you have to asses several different "situations" the compiler might get what you wanted to do wrong, and it would take a it longer to look for those situations rather than simply looking for a semicolon at the end of the statement. Some higher level languages try to reduce semicolon use or remove it altogether in order to save a few keystrokes, this languages are more oriented toward the comfort of the programmer and generally come with all sort of syntactic sugar; one could argue that not using semicolons is a kind of syntactic sugar. The use or not of a semicolon in a language should be in according to what the language is trying to accomplish, Languages like C and C++ are mostly about performance, Java and C# are a bit higher in the abstraction sense than C and C++ and then we have things like Scala, Python and Ruby, which are made mostly to make programming more comfortable a the cost of performance,(Ruby openly admits this, and it's very pronounced on Python). So why do some languages "need" semicolons?

  • Makes compiling easier
  • The designer of the language thinks it's more consistent
  • Historical reasons (Java, C# and C++ are also C's children for example)

and one last thing is that Javascript actually adds the semicolons during compile or before IIRC, so it's not actually semicolon free.

share|improve this answer

Your Answer


