views:

113

answers:

6

Why is it that in most programming languages it is required to have a semicolon after statements but not after things like if elseif and else.

Do the compilers all look out for newlines? If that's true then why wouldn't they do that for all statements?

Am I missing something? It really doesn't make sense to me...

+2  A: 

Because compilers for those languages don't consider whitespace for statement termination. A statement has to be terminated some how and it's done with a semicolon. You can write all code (although it would be a horrible, horrible idea) on one line as long as you terminate statements correctly.

jlafay
+11  A: 

Usually the semicolon is required because the compilers ignore most whitespace. It's not needed after statements like if, elseif, else because it's not the end of the statement. Those are only complete statements when followed by a statement or block of statements.

Colin
Technically a 'Statement Block' is a 'Statement'. So you can simply say "followed by a statement"
Martin York
+2  A: 

Some compilers ignore whitespace and use the semicolon to determine statements, like variable assignments vs. if {} code blocks.

Other languages, like python, use whitespace to find statements and if blocks.

jlindenbaum
+1  A: 

Why use ';' rather than '\n' to terminate a statement.

Very few languages uses white space as something that affects the semantics of a language (new line being white space). This is because it is very error prone for the developer (as the white space is for all intense invisible).

Look at languages that do use white space to affect the semantics and you will see the effect on the programmers (special tools are usually built to help align things etc). Lots of bald developers (male and female) who have torn out their hair if frustration because they inserted a space instead of a tab and this caused the loop to be exited early. :-)

Also by not using white space to alter semantic meaning of the language you can now use white space solely for the human readers of the language to try and format the code into a form that makes it easy to read (for the human) (or you can abuse the white space to hide meaning).

Why are not all statements white space

Its just that you need a way to check the end of statement.
Some things it is obvious where the end of the statement is and you do not need a an extra delimiter to allow the parser to detect the end of a statement.

Martin York
+2  A: 

It relates to the difference between statements and expressions. Languages like C require a block to contain a series of statements. Control flow structures like if and while are statements all on their own:

void foo() {
    if (bar) {
        /* ... */
    }
    while (baz) {
        /* ... */
    }
}

There are no semicolons needed here because everything inside foo() is a statement. The question is, what if you want an expression like bar() in a place where a statement is expected? The C grammar says you can do that by adding a semicolon after the expression (in other words, an expression followed by ; is a statement).

So, this isn't valid:

void foo() {
    1 + 2
}

Because 1 + 2 is an expression. You need to turn it into a statement:

void foo() {
    1 + 2;
}

To fully understand what's going on, it's important to note that a block (something in curlies like { foo(); } is also a statement, and that the grammar for if is something like:

if ( <condition> ) <statement> (else <statement>)?

This is why if statements can have blocks for bodies or single statements: a block is a single statement.

munificent
+1  A: 

A source code is a set of statements. We have to delimitate the statements, using delimitators. If we use the newline as the delimitator, we can't structure our codes. Very long lines will only be readable by scrolling. (to avoid scrolling, long lines usually are split.) For example:

ParserUtils.RefreshProperty(m_cfg.PORTAL_ID, ParserUtils.CreateHashFromUrl(strDetailLinkUrl), Convert.ToInt32(m_cfg.Type), strPrice, strAddress, strStreet, strPostCode, strFeatures, strDescription, strImgFile, strBedrooms, strReception, strBath, strStatus, strLink, strPropType, strOutside, strTenure, strKeywords, strFullText, strContactInfo, m_ieBrowser.URL);

is very ugly and instead of this, we split this line to several lines to make this more readable:

    ParserUtils.RefreshProperty(m_cfg.PORTAL_ID,
    ParserUtils.CreateHashFromUrl(strDetailLinkUrl),
    Convert.ToInt32(m_cfg.Type), strPrice,
    strAddress, strStreet, strPostCode, strFeatures, strDescription, strImgFile,
    strBedrooms, strReception, strBath, strStatus, strLink, strPropType,
    strOutside, strTenure, strKeywords, strFullText, strContactInfo,
    m_ieBrowser.URL);

This would be impossible if newline was the delimitator. If's, while's and for's would be a total mess if newline was the operator. Consider this code:

for (int i = 0; i < n; i++)
    {
        if (i % 2 == 0)
        {
            System.out.println("It can be divided by two");
        }
        {
            System.out.println("It can't be divided by two");
        }
    }

If newline was the operator instead of the semicolon, this source code would be very ugly:

for (int i = 0
     i < 0
     i++) { if (i % 2 == 0) { System.out.println("It can be divided by two")
                          } { System.out.println("It can't be divided by two")
                          } }

This code is difficult to read, and it's logically valid as the delimitator. For example, my wife writes my tasks on a paper (an algorithm) like this:

Buy food(Bread, Meat, Butter),
Go and pay the taxes,
Call your mother, because she wants to talk to you

These tasks are separated by commas, but notice that parameters are separated by commas too. We have to make a difference between a comma as a parameter separator and a comma as a delimitator of tasks, because the computer is not as intelligent as human beings. As a conclusion, the separator of tasks is a bigger comma than the separator of parameters. That's why the delimitator of statements is a semicolon and the delimitator of parameters is a comma.

Lajos Arpad