Wednesday, March 15, 2006

The Semicolon

Introduction

The semicolon, ;, was invented by Aldus Manutius the elder (1450-1515), an Italian printer. Manutius used the semicolon to separate words opposed in meaning and to mark off mutually dependent statements (http://en.wikipedia.org/wiki/Semicolon).

The semicolon in Java works much the same way as the punctuation mark in English. The punctuation mark terminates a sentence in English, so that you may write several sentences following each other. The semicolon in Java terminates a statement, so that you may write several statements following each other in sequence.

Discussion

So, why does most programming languages use the semicolon instead of the punctuation mark? The answer is that the semicolon is supposed to do several things. The semicolon terminates statements, separate statements and concatenates statements. Besides, the punctuation mark is used for other purposes.

The Order of Execution

Statements are executed one at a time, from top to bottom, left to right. The computer gets one statement and executes it, and then gets the next statement followed by the semicolon.

Key Concept: A sequence is ordered statements that are executed in the same order as written. Statements are sequenced (ordered) by the semicolon.

Key Concept: Any statement which does not perform an explicit jump, actually performs an implicit jump to the next statement because of the semicolon.

Note: The semicolon is not an operator because operators are used to combine factors in an expression. That is, operators are not used to combine statements.

Consider the code:
class StarTrek {
    public static void main(String[] args) {
        System.out.println("              Space, the final frontier.");
        System.out.println();
        System.out.println("   These are the voyages of the Starship Enterprise.");
        System.out.println("Its continuing mission: to explore strange new worlds;");
        System.out.println("      to seek out new life and new civilizations;");
        System.out.println("      to boldly go where no one has gone before.");
    }
}
[user]$ javac StarTrek.java
[user]$ java StarTrek
              Space, the final frontier.

   These are the voyages of the Starship Enterprise.
Its continuing mission: to explore strange new worlds;
      to seek out new life and new civilizations;
      to boldly go where no one has gone before.

Obviously, the order of execution is normally of importance for the result. The Star Trek mission statement would not give any meaning stated in a different order.

Readability

Semicolons may make a program look cluttered, but with semicolons, long statements can run over several lines with new line almost anywhere you prefer. With semicolon, several statements can also be placed on the same line, but this should generally be avoided. It was usually done to save print paper on old computers without a monitor. A statement should be written beneath the statement above, because of readability.

How to terminate statements

Placement

In Java, each statement must be followed by the ; character. Even the last statement must be followed by the semicolon, so that you do not forget to add the semicolon if you later have to move the statement. It would also be confusing if some statements need semicolon and some statements don't. In the programming language Pascal, you don't need semicolon after the last statement. Now, what happens if you later have to move the last statement and you sort of forget adding the semicolon? This is a constant source of compilation errors in Pascal. The solution is to add an empty statement, the semicolon, after the last statement, which by "coincidence" is what you have to do in Java anyway.

Each individual statement is terminated by a semicolon. (The C programming language, Second Edition, 1988, Brian W. Kernighan, Dennis M. Ritchie. Prentice Hall, s. 10.)

The semicolon is a statement terminator in Java, rather than a separator. Java got this from C: "In C, the semicolon is a statement terminator, rather than a separator as it is in languages like Pascal." (The C programming language, Second Edition, 1988, Brian W. Kernighan, Dennis M. Ritchie. Prentice Hall, s. 55.).

In Java, each statement must be terminated with the semicolon. Even the last statement must be terminated with semicolon, because it is easy to forget to add the semicolon if you later have to move some statements. Also, it would be confusing if some statements need semicolon and some statements don't.

The goon Symbol

The semicolon means that after this statement has been executed and terminated, then go on to the next statement following this statement. (Lykkenborg 2005)

C got the semicolon from ALGOL 68. The ALGOL 68 semicolon is called the GOON SYMBOL pronounced "go on" (http://www.flashdance.cx/txt/Hacking_text/Hacker_Ordlista.txt).

The 'go on symbols' (better known as semicolons) constitute the mortar which bind the bricks together (Lindsey & Meulen 1974).

  3.0.7. Sequencing tokens
  go on symbol        ;
  completion symbol   .
  go to symbol        go to
(Wijngaarden et al. 1968).

The Empty Statement

The empty statement denotes the null action (Wirth 1983).

The Sequence

"The most basic structure is the sequence. A computation is a sequence of actions, where each action is specified by a statement, and is executed after the preceding action is completed. This strict sequentiality in time is an essential assumption of sequential programming. If a statement S1 follows S0, then we indicate this sequentiality by a semicolon S0; S1" (Wirth 1983:17).

The Statement Separator

"This statement separator (not terminator) indicates that the action specified by S0 is to be followed immediately by the action corresponding to S1." (Wirth 1983:17).

References

Author Wirth, Niklaus
Year 1983
Title Programming in Modula-2, Second Edition
Place Berlin Heidelberg New York
Publisher Springer-Verlag
Retrieved
URL

Author Wijngaarden, A. van (editor), Mailloux, B. J., Peck, J.E.L. & Koster, C.H.A.
Year 1968
Title Draft report on the algorithmic language ALGOL 68, third printing
Place Amsterdam
Publisher Mathematisch centrum
Retrieved
URL

Author Lindsey, C.H., Meulen, SG. van der
Year 1974
Title Informal Introduction to Algol 68
Place
Publisher
Retrieved
URL

No comments: