Your First ALGOL 60 Programs
7.1 Sum of squares
Things to notice:
Declarations come at the start of the block, before any statement.
Statements are separated (not terminated) by semicolons — there is no ; before 'end' (an extra one is legal: it creates an empty statement).
outstring, outinteger and outreal take a channel number first; in MARST, channel 1 is standard output and channel 0 is standard input.
'begin'
'comment' compute 1^2 + 2^2 + ... + n^2;
'integer' i, n, sum;
n := 10;
sum := 0;
'for' i := 1 'step' 1 'until' n 'do'
sum := sum + i * i;
outstring(1, "Sum of squares = ");
outinteger(1, sum);
outstring(1, "\n")
'end'7.2 Reading input
inreal, ininteger and inchar come from the Modified Report's standard environment; they are not part of the Revised Report itself (§19).
'begin'
'real' x, y;
outstring(1, "Enter two numbers: ");
inreal(0, x);
inreal(0, y);
outstring(1, "Product = ");
outreal(1, x * y);
outstring(1, "\n")
'end'7.3 Celsius to Fahrenheit table
'begin'
'integer' c;
'for' c := -20 'step' 10 'until' 100 'do'
'begin'
outinteger(1, c);
outstring(1, " C = ");
outreal(1, c * 9 / 5 + 32);
outstring(1, " F\n")
'end'
'end'7.4 Comments
ALGOL 60 has three comment conventions:
'comment' ... ; may appear after 'begin' or after a ;.
Any sequence of symbols following 'end' up to the next ;, 'end' or 'else' is a comment. (This is a notorious trap: forgetting a semicolon after 'end' can silently "comment out" the next statement's beginning — see §43.)
In procedure headings, ) *letter string* :( may replace a comma in a parameter list as a kind of comment: p(a) Result: (b).
'comment' any text not containing a semicolon;
'begin' 'comment' a comment right after begin;
x := 1
'end' text after end up to the next semicolon, end, or else is ignored;7.5 Publication form of the same program
In a book you would see:
begin integer i, n, sum; n := 10; sum := 0; for i := 1 step 1 until n do sum := sum + i↑2; outinteger(1, sum) end
Being able to read publication form is essential for using the thousands of algorithms published in CACM, *Numerische Mathematik* and the *Handbook for Automatic Computation* (Wilkinson & Reinsch, 1971).