0

Possible Duplicate:
Why are statements in many programming languages terminated by semicolons?

I just found out that R Programming Language, which is somewhat belong to the C family (I'm not sure about this but it uses {} instead of begin end), uses a new line as a delimiter for a new line of code instead of the semi-colon ;.

Why is some programming language designed to use a new line instead of a symbol like semi-colon which I think will make the code more readable?


Phyton

total = item_one + \
        item_two + \
        item_three

C

total = item_one +
        item_two +
        item_three;

3 Answers3

5

The idea behind semantic whitespace in Python is that the program structure as seen by the interpreter should match the structure as seen by humans as closely as possible. Consider the following C program:

if ( foo )
    bar;
    baz;

At first glance, you'll assume that bar and baz are both conditional; they aren't, however, due to misleading indentation. The C way of thinking is "make your whitespace match semantics". The Python way is more along the lines of thinking DRY: "whitespace already expresses program structure, just parse it".

thiton
  • 5,348
4

Any answer to this is going to be subjective. There was a big discussion about why the semicolon was used to terminate statements in this question. The bottom line is that any scheme can be made parseable and it is up to the language designer to choose their own statement termination scheme. This choice will be influenced by the emotional baggage that they attach to each scheme. Some people have a pathological loathing for semicolons or other explicit statement termination characters, while others see utility in having an explicit, visible termination character.

uɐɪ
  • 2,981
  • 1
  • 18
  • 17
3

The semicolon in C and languages that adopt its syntax is used to terminate statements. Put simply it's the symbol the compiler looks for to know where a statement ends, and by association where the next one begins.

It's mostly a quirk of the language, a convention. Whether it makes code more readable or not depends a lot on your personal preferences, I don't think there is a valid metric that can be applied objectively. Wikipedia has an extensive list of languages organized per their statement terminator, and some of the popular newline terminated / separated languages are:

  • Haskell,
  • BASIC,
  • Eiffel,
  • Fortran,
  • Visual Basic

and others.

You can find a very interesting discussion on whether semicolons or newlines should be used as statement separator/terminator here: http://www.cs.nmsu.edu/~jeffery/godiva/semi.html. Sum of the discussion is that Larry Wall really likes his semicolons ;)