Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [151]
The first version of the language, known as ALGOL 58, was designed by an international committee in 1957 and 1958. It was improved two years later in 1960, and the revised version was named ALGOL 60. Eventually, there was an ALGOL 68, but for this chapter I'll be using the version of ALGOL as described by the document "Revised Report on the Algorithmic Language ALGOL 60" finalized in 1962 and first published in 1963.
Let's write some ALGOL code. We'll assume we have an ALGOL compiler named ALGOL.COM that runs under CP/M or perhaps MS-DOS. Our first ALGOL program is a text file named FIRST.ALG. Notice the ALG file type.
An ALGOL program must be enclosed within the words begin and end. Here's a program that displays a line of text:
begin
print ('This is my fist ALGOL program!');
ende
You can run the ALGOL compiler by specifying the FIRST.ALG program like this:
ALGOL FIRST.ALG
The ALGOL compiler will probably respond by displaying something similar to the following:
Line 3: Unrecognized keyword 'ende'.
A compiler is pickier about spelling than an old-fashioned English teacher. I misspelled the word end when I was typing the program, so the compiler tells me that the program has a syntax error. At the time it encountered ende, it expected to find a keyword, which is a word that it recognizes.
After fixing the problem, you can run the ALGOL compiler again. Sometimes a compiler will create an executable directly (named FIRST.COM, or perhaps FIRST.EXE under MS-DOS); sometimes you need to perform another step. Regardless, you'll soon be able to run the FIRST program from the command line:
FIRST
The FIRST program responds by displaying
This is my fist ALGOL program!
Oops! Another spelling error. This is one that the compiler could not be expected to find. For that reason it's called a run-time error—an error that's apparent only when you run the program.
As is probably obvious, the print statement in our first ALGOL program displays something on the screen, in this case a line of text. (The program is thus the ALGOL equivalent of the CP/M assembly-language program shown earlier in this chapter.) The print statement isn't actually part of the official specification of the ALGOL language, but I'm assuming that the particular ALGOL compiler we're using includes such a facility, sometimes called a built-in function. Most ALGOL statements (but not begin and end) must be followed by a semicolon. The indenting of the print statement isn't required, but indenting is often used to make the structure of the program clearer.
Let's assume now that you want to write a program that multiplies two numbers. Every programming language includes the concepts of variables. In a program, a variable's name is a letter, a short sequence of letters, or even a short word. In reality, the variable corresponds to a memory location, but in the program it's referenced by means of a name, not a numeric memory address. This program has three variables named a, b, and c:
begin
real a, b, c;
a := 535.43;
b := 289.771;
c := a x b;
print ('The product of ', a, ' and ', b, ' is ', c);
end
The real statement is called a declaration statement. It indicates that you want to declare the presence of variables in your program. In this case, the variables are named a, b, and c and are real or floating-point numbers. (ALGOL also supports the keyword integer to declare integer variables.) Usually programming languages require that variable names begin with a letter. Variable names can also contain numbers, just as long as the first character is a letter, but they must not contain spaces or most other characters.