What allows the some languages to do this and others to not? Why not keep a uniform system of adding the semicolon to the end?
Robert gave a good answer regarding Swift, I'll try to add bit more about parsing in general and why to use or not semicolons.
When you compile the program, there are several steps before your source file is turned into executable code, one of the first being reading and parsing. During parsing your code is transformed from a sequence of characters into a structured representation according to the grammar of the language. For this it's necessary that the elements that compose your code (variables, constants, declarations, etc.) are somehow identified and separated. When you make a declaration like:
int someNumber = 0;
It's just a sequence of characters that somehow needs to be turned unto the concept "create a 'thing' called someNumber
of type int
and assign 0
to it".
This requires identifying where exactly the statement begins and ends. Imagine if you had this:
int someNumber = 0;
int otherNumber = 1;
You would need to know that those are two different declarations. The ;
in C-like languages is used for that, so the parser reads the characters until it finds one, and attempts to understand what was just read as a single statement. This allows, as Robert said, to have several statements in the same line, like:
int someNumber = 0; int otherNumber = 1;
Which has exactly the same effect as writing them in two lines.
In other languages, like Python, you are expected to put each statement in a different line.
x = 3
if x < 10:
print 'x smaller than 10'
else:
print 'x is bigger than 10 or equal'
So there is no need to add a ;
, because the language itself prevents you from adding statements in the same line!
Yet other languages, like LISP or Scheme, have a very different way of grouping statements (ok, they are not really statements, but let's ignore that for now). So you have just parenthesis for everything:
(define (do-stuff x)
(begin
(display (format "value: ~a" x))
x))
Again, you don't need the ;
just because you use the same symbols ()
, (
) for things that in other languages are {
, }
, :
, etc. Someone who only knows Lisp syntax might ask you: Why do you need ;
at the end of your statements?
Just to add a stranger example: OCaml not only has a semicolon at the end of statements... it has two!
let rec fact x =
if x <= 1 then 1 else x * fact (x - 1);;
Why? I don't know. :)
Different languages are designed with different purposes and different philosophies. If all languages shared the same syntax and conventions, they would probably only allow you to do the same things in the same way.