Chapter 3: Syntax

Each language has many syntax rules. For example, if you wanted the computer to display two different things you could write:

print "I have a dog.  His name is Bort."

or you could write:

print "I have a dog.  " + "His name is Bort."

The second example has two strings, each enclosed in quotation marks. There’s a plus (+) sign in between them, which means, “Attach the two strings together, turning them into a single string.” (Attaching two strings is called concatenation.) The above two statements will display the same thing:

I have a dog.  His name is Bort.

In the above examples there’s no reason why you’d use a + and two strings when you could just use a single string, as in the first example, but later we will see cases where concatenation of strings is necessary. The languages Python and Java use plus (+) for string concatenation, Perl and PHP use the period (.), and Visual Basic uses ampersand (&). Again, these differences are superficial; what’s important is the concept of string concatenation and of language syntax to do things like concatenation. Separate in your mind an underlying concept and a particular language’s way of expressing that concept.

You might ask why people invented languages with superficial differences like these. These differences are usually the result of trade-offs, sometimes for clarity, sometimes for ease of programming, sometimes to get the program to execute as fast as possible. One language designer may have felt that a plus sign was the clearest way to attach two strings together. Another may have decided that this was ambiguous with the meaning of plus in mathematics for numbers, and chose the ampersand instead. A third thought that the ampersand meant “and”, which should be used for logic queries, not string manipulation, and chose the period instead. A fourth designer decided that since there are many ways to attach two strings together (internally to the computer), they didn’t want to choose a single one for the programmer, and instead forced the programmer to write several lines of detailed code to accomplish this simple task.

There are many different philosophies in language design. The programming language Perl, for example, tries to be very terse, so you can accomplish a great deal with very little typing. This is an advantage to those who like to type less, but a disadvantage to those who find the terse code cryptic. The programming language Java is all about safety, and it erects a safety net around the program so that a badly-written program will not crash. This causes the resulting program to run more slowly, so whether you use Java depends on whether you value speed or safety. The language C is the opposite: it has no safety net and allows your program to run as fast as possible. Since a safety net can save you (the programmer) time while writing the program, you can see the trade-off as saving the computer time or saving you time.

There’s an old tradition of demonstrating a programming language’s syntax by writing a program that displays the text “Hello, world!” In the language we’re going to use in this book, that would look like this:

print "Hello, world!"

The list of hello world programs page at Wikipedia shows this program in over 350 programming languages. Skim it to see the huge variety in syntax.