ALGOL 68: Design, Orthogonality & First Programs

The ALGOL 68 Report lists its aims: completeness and clarity of description, orthogonal design, security (strong typing, checked at compile time where possible), efficiency (static mode checking, no need for run-time type tests except for unions), and suita…

24.1 Design goals

The ALGOL 68 Report lists its aims: completeness and clarity of description, orthogonal design, security (strong typing, checked at compile time where possible), efficiency (static mode checking, no need for run-time type tests except for unions), and suitability for a wide range of applications — not just numerical computing.

24.2 Orthogonality

Orthogonality means a small number of independent primitive concepts that combine in every meaningful way, with no special cases:

Any mode can be the element of a row, a field of a structure, a parameter or result of a procedure, or the target of a reference.

Every construct that yields a value can be used where a value is expected: blocks, conditionals, loops (yielding VOID), assignments.

Procedures are values; operators are user-definable; modes are user-definable.

24.3 Everything is an expression

ALGOL 68 has no separate notion of "statement". A unit yields a value (possibly VOID):

BEGIN
   INT a = 7, b = 3;
   INT max = IF a > b THEN a ELSE b FI;          # conditional yields a value #
   INT m2 = (a > b | a | b);                     # the same in brief form #
   INT sum = BEGIN INT s := 0; FOR i TO 10 DO s +:= i OD; s END;   # a block yields its last unit #
   INT x;
   INT y = x := 42;                              # an assignment yields its destination; y = 42 #
   print((max, m2, sum, y, newline))
END

24.4 First programs

Sum of squares: Reading input: Temperature table:

BEGIN
   INT n = 10;
   INT sum := 0;
   FOR i TO n DO sum +:= i * i OD;
   print(("Sum of squares 1..", whole(n, 0), " = ", whole(sum, 0), newline))
END
BEGIN
   REAL x, y;
   print("Enter two numbers: ");
   read((x, y));
   print(("Product = ", fixed(x * y, 0, 4), newline))
END
BEGIN
   FOR c FROM -20 BY 10 TO 100 DO
      REAL f = c * 9 / 5 + 32;
      print((whole(c, 4), " C = ", fixed(f, 7, 1), " F", newline))
   OD
END

24.5 Printing numbers nicely

print writes numbers in a default, fixed-width format that includes a sign (e.g. an INT prints as +42 padded on the left). For human-friendly output use:

whole(42, 0) 42 (width 0 = as narrow as possible) whole(42, 6) fixed(x, w, d) fixed(pi, 0, 3) fixed(pi, 8, 2) float(x, w, d, e) float(12345.678, 12, 4, 3)

A negative width (e.g. whole(42, -6)) forces a sign to be shown.

24.6 Comments and pragmats

(The Report also uses ¢ ... ¢; a68g accepts the ASCII forms shown.)

# a comment between hash signs #
CO another comment CO
COMMENT yet another COMMENT
PR a pragmat (instructions to the implementation) PR

24.7 Program structure

A program is a closed clause — BEGIN ... END or ( ... ) (the two are interchangeable). Inside, a serial clause is a sequence of declarations and units separated by ;, whose value is the value of its last unit. a68g also accepts a bare serial clause without the enclosing BEGIN ... END.