Beginner

Control Flow in ALGOL 60

Learn conditionals, for loops, while loops, and structured control flow in ALGOL 60 with practical Docker-ready examples

Introduction

Control flow is where ALGOL 60’s lasting influence becomes most visible. When the language was designed in 1960, structured programming was not yet a household idea — most contemporaries leaned heavily on labels and goto. ALGOL 60 quietly changed that by giving programmers a small, composable kit: if-then-else, a remarkably expressive for statement, and begin-end blocks that let any conditional or loop body contain a whole sequence of statements.

As an imperative, procedural, structured language with static, strong typing, ALGOL 60 expects you to declare control variables explicitly and use them within well-defined scopes. The branching constructs introduced here — if-then-else, the unified for statement with step/until, while, and explicit value lists — are direct ancestors of what you write today in Pascal, C, Java, and Python.

In this tutorial you’ll see how ALGOL 60 expresses conditional branching, counted iteration, descending loops, value-list iteration, and while-style loops — all from one carefully designed for statement.

Conditional Statements

The if-then-else statement chooses between two alternatives based on a Boolean expression. ALGOL 60 also allows chained else if branches, which is how nested classification logic is written.

Note one important rule: a bare if cannot directly follow then (the dangling-else problem). After then you need an unconditional statement (or a begin-end block). Chained else if is safe because the second if appears after else.

For Loops: Three Forms in One Statement

ALGOL 60’s for statement is unusually flexible. A single syntax supports three iteration styles:

  • Step-until form: for v := E1 step E2 until E3 do S — counted iteration (the ancestor of C’s for(;;))
  • List form: for v := E1, E2, E3 do S — iterate over an explicit list of values
  • While form: for v := E while B do S — repeat until a Boolean condition is false

These forms can even be combined in a single for statement, separated by commas. We’ll demonstrate each form below.

A Comprehensive Example

We’ll put all four control-flow constructs into a single program. Create a file named control_flow.alg:

begin
  integer i, n;

  comment if-then-else: simple two-way branch;
  n := 7;
  if n > 0 then
    outstring(1, "positive")
  else
    outstring(1, "non-positive");
  outstring(1, "\n");

  comment chained else-if for classification;
  if n > 10 then
    outstring(1, "big")
  else if n > 5 then
    outstring(1, "medium")
  else
    outstring(1, "small");
  outstring(1, "\n");

  comment for loop, step-until form (counted iteration);
  outstring(1, "count:");
  for i := 1 step 1 until 5 do
    begin
      outstring(1, " ");
      outinteger(1, i)
    end;
  outstring(1, "\n");

  comment for loop counting down with negative step;
  outstring(1, "down: ");
  for i := 5 step -1 until 1 do
    begin
      outinteger(1, i);
      outstring(1, " ")
    end;
  outstring(1, "\n");

  comment for loop over an explicit value list;
  outstring(1, "powers:");
  for i := 2, 4, 8, 16 do
    begin
      outstring(1, " ");
      outinteger(1, i)
    end;
  outstring(1, "\n");

  comment for-while form acts as a while loop;
  i := 1;
  outstring(1, "while:");
  for i := i while i < 20 do
    begin
      outstring(1, " ");
      outinteger(1, i);
      i := i * 2
    end;
  outstring(1, "\n")
end

A few details worth noting:

  • The control variable i is declared once at the top of the block. ALGOL 60 requires you to declare variables before using them.
  • Each loop body is a compound statement wrapped in begin-end. Without the block, the loop would govern only the single next statement.
  • Statements inside a block are separated by semicolons, but the last statement before end has no trailing semicolon. This is a defining feature of ALGOL 60 syntax.
  • In the step -1 until 1 form, the loop runs as long as (i - 1) * sign(-1) <= 0, so it correctly stops at i = 1 and does not overshoot.
  • In the for i := i while i < 20 do ... form, the right-hand i is re-evaluated each iteration. Because we update i inside the body, this acts like a classic while loop: print, double, repeat.

A Note on goto

ALGOL 60 also supports labels and goto, inherited from earlier languages. The structured constructs above are almost always clearer, but goto remains in the language. Modern style — and most structured-programming advice from Dijkstra onwards — recommends preferring the if and for constructs you’ve just seen.

Running with Docker

Pull the image and run the program with GNU MARST, which compiles ALGOL 60 to C and links against the ALGOL runtime library:

1
2
3
4
5
# Pull the official image
docker pull codearchaeology/algol60:latest

# Compile and run the control-flow example
docker run --rm -v $(pwd):/app -w /app codearchaeology/algol60:latest algol60-run control_flow.alg

The algol60-run helper inside the image performs the two-stage translation (ALGOL 60 → C → executable) and runs the result, so you see the output directly in your terminal.

Expected Output

positive
small
count: 1 2 3 4 5
down: 5 4 3 2 1 
powers: 2 4 8 16
while: 1 2 4 8 16

Key Concepts

  • Structured branching: if-then-else selects one of two statements; chained else if forms a classification ladder. A raw if cannot directly follow then — wrap it in begin-end or place it after else.
  • One for, three styles: Step-until, explicit list, and while-condition forms all live inside the single for statement and can be mixed in one loop header.
  • Block bodies: Loop and conditional bodies that contain more than one statement must be wrapped in begin-end. The compound statement is the ancestor of { ... } in C-family languages.
  • Semicolons are separators, not terminators: Inside a block, semicolons separate statements; the statement just before end is not followed by a semicolon.
  • Explicitly typed control variables: Loop counters must be declared (typically integer) before use — ALGOL 60’s static type system catches missing declarations at compile time.
  • Direction-aware step-until: The step expression can be negative, and the loop terminates correctly in either direction without special syntax.
  • for-while is the only while loop: There is no dedicated while keyword; the for v := E while B do S form fills that role.
  • Historical influence: Pascal’s for...to...do, C’s for(init; cond; incr), and Python’s for x in [...] all trace their lineage back to ALGOL 60’s unified for statement.

Running Today

All examples can be run using Docker:

docker pull codearchaeology/algol60:latest
Last updated:

Comments

Loading comments...

Leave a Comment

2000 characters remaining