ZetCode

Algol 68 basics

last modified November 13, 2026

This article covers the everyday parts of Algol 68: declaring values and variables, printing them, making decisions, repeating work, and writing procedures. The examples were tested with Algol 68 Genie 2.8.4 and are run with a68g program.a68.

Declarations and values

Every name is declared with its mode. A declaration with = binds a name to a value and creates a constant; a declaration with := creates a variable. The mode must match the value, although an integer is widened to a real automatically.

values.a68
BEGIN
  INT count := 3;
  REAL price := 2.5;
  STRING name := "tea";
  print((name, ": ", whole(count, 0), " x ", fixed(price, 0, 2), newline))
END

count, price, and name are variables, so the program could change them later. whole(count, 0) converts the integer to a string without padding, and fixed(price, 0, 2) rounds the real to two decimal places. Both conversions are needed here because print otherwise uses its own fixed-width layout for numbers.

$ a68g values.a68
tea: 3 x 2.50

Assignment and arithmetic

The assignment operator is :=. An operator followed by := combines an operation with an assignment, so total +:= 5 adds five to total. The usual arithmetic operators are +, -, *, /, and ** for exponentiation.

assign.a68
BEGIN
  INT total := 10;
  total +:= 5;
  total *:= 2;
  print((whole(total, 0), newline))
END

The first assignment raises total to 15 and the second doubles it to 30. Because total is an integer, * performs integer multiplication and the result stays an integer.

$ a68g assign.a68
30

Printing with printf

printf gives more control than print. Its first argument is a format between dollar signs, followed by the values that the format consumes. The letter g prints a value, l starts a new line, and g(0) asks for an integer without the usual sign and padding.

print.a68
BEGIN
  printf(($"Name: "gl$, "Ada"));
  printf(($"x = "g(0)l$, 42));
  printf(($"pi = "gl$, fixed(3.14159, 0, 2)))
END

The first line prints a string, the second an integer, and the third a pre-formatted real. A string produced by fixed is printed with g, not with g(0), because g(0) expects a number. Statements are separated by semicolons; the last one has none.

$ a68g print.a68
Name: Ada
x = 42
pi = 3.14

Comments

Comments are enclosed between two # characters and may span several lines. They can appear before a declaration, after a statement, or between statements.

comments.a68
# single-line comment #
BEGIN
  # a comment may span
     several lines #
  print(("comments are ignored", newline))
END

The interpreter skips both comments. The example also shows that the layout of a program is free: indentation is only a convention, and the semicolon, not the line break, separates statements.

$ a68g comments.a68
comments are ignored

Conditionals

An IF clause starts with IF and a condition, and ends with FI. The THEN branch runs when the condition is true; ELIF tests another condition, and ELSE supplies the remaining branch.

decide.a68
BEGIN
  INT score := 75;
  IF score >= 90 THEN
    print(("grade A", newline))
  ELIF score >= 75 THEN
    print(("grade B", newline))
  ELSE
    print(("grade C", newline))
  FI
END

The conditions are tested from top to bottom. A score of 75 fails the first test but passes the second, so the program prints grade B. The comparison operators include =, /=, <, <=, >, and >=.

$ a68g decide.a68
grade B

Loops

A counted loop uses FOR, TO, DO, and OD. A conditional loop uses WHILE in place of the counter. Both forms may contain a serial clause, and both end with OD.

loops.a68
BEGIN
  INT sum := 0;
  FOR i TO 5 DO
    sum +:= i
  OD;
  print(("sum = ", whole(sum, 0), newline));
  INT n := 3;
  WHILE n > 0 DO
    print((whole(n, 0), " "));
    n -:= 1
  OD;
  print((newline))
END

FOR i TO 5 counts from 1 to 5, so the sum is 15. The WHILE loop prints the numbers from 3 down to 1 and stops when n reaches zero. The counter i exists only inside the loop.

$ a68g loops.a68
sum = 15
3 2 1

Procedures

A procedure is declared with PROC, a name, a parameter list, and a result mode. The body follows a colon. A parameter declared with REF receives a name, so the procedure can change the caller's variable; a parameter without REF receives a copy.

procs.a68
PROC square = (INT n) INT: n * n;
PROC double = (REF INT n) VOID: n *:= 2;

BEGIN
  print(("square = ", whole(square(6), 0), newline));
  INT x := 7;
  double(x);
  print(("doubled = ", whole(x, 0), newline))
END

square returns an INT, while double returns VOID and changes its argument through the REF parameter. Calls are written like the built-in procedures: the arguments go in parentheses, and a parameterless procedure can be called without them.

$ a68g procs.a68
square = 36
doubled = 14

Next steps

The variables article explains the difference between constants, variables, and references in more depth. The program structure article covers the clause forms, and the modes article describes the values that declarations can use.

Source

The Algol 68 Genie project

Revised Report on the Algorithmic Language Algol 68

In this article we have written declarations, assignments, formatted output, conditionals, loops, and procedures in Algol 68. The variables article continues with references and storage, while program structure shows how the same units combine into larger clauses.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Algol 68 tutorials.