Labels, goto & Switches
Any statement can carry one or more labels.
13.1 Labels and goto
Any statement can carry one or more labels. Labels are identifiers (the Revised Report also allowed unsigned integers like 10:, removed by the Modified Report):
Labels are local to the block in which they occur. A goto may leave blocks (and procedures), unwinding their local storage — a powerful, structured form of non-local exit — but may not enter a block from outside.
'begin'
'integer' n;
n := 0;
again:
n := n + 1;
outinteger(1, n);
'if' n < 5 'then' 'goto' again;
outstring(1, "\ndone\n")
'end'13.2 Designational expressions
A designational expression yields a label: a label identifier, a switch designator s[i], or a conditional designational expression:
'goto' 'if' x > 0 'then' positive 'else' nonpositive;13.3 Switches
A switch is an array of labels — ALGOL 60's computed goto / jump table:
If the subscript is out of range, the Revised Report makes the goto a dummy statement (falls through); the Modified Report makes it undefined. Switch elements may themselves be designational expressions, including other switches and conditionals.
'begin'
'integer' day;
'switch' handler := monday, tuesday, other;
ininteger(0, day);
'goto' handler[day];
monday: outstring(1, "Monday\n"); 'goto' finish;
tuesday: outstring(1, "Tuesday\n"); 'goto' finish;
other: outstring(1, "Some other day\n");
finish:
'end'13.4 Labels and switches as parameters
Procedures can receive labels and switches, enabling error exits:
A call safe divide(x, y, z, div error) jumps straight to div error in the caller's block if y is zero — a precursor of exceptions.
'procedure' safe divide(a, b, result, failed);
'value' a, b; 'real' a, b, result; 'label' failed;
'begin'
'if' b = 0 'then' 'goto' failed;
result := a / b
'end';13.5 goto and structured programming
goto is essential in ALGOL 60 because the language has no while, repeat, break or case statements. Dijkstra's 1968 letter "Go To Statement Considered Harmful" argued for disciplined control structures; ALGOL W, Pascal and ALGOL 68 added while and case to reduce the need for goto. Good ALGOL 60 style uses goto only to emulate those structures (loops with exits, case selection, error exits).
🧪 Part I Exercises
Write the Revised Report BNF rule for <unsigned number> from memory, then check it against the Report. Extend it (in BNF) to signed numbers.
Write a program in MARST ALGOL 60 that reads n and prints n! using a for loop; note at what n integer overflow occurs on your system.
Rewrite the publication-language program in §7.5 in three hardware representations: MARST quote stropping, upper-case quote stropping, and reserved-word style.
Write a program that prints all Pythagorean triples with sides up to 50 using nested for loops.
Demonstrate that the step and until expressions are re-evaluated each iteration: write a loop whose body changes its own limit and explain the output.
Implement integer mod, gcd and lcm without a mod operator, using only ÷ (MARST %).
Write a block-structured program with three nested blocks that each declare x, and print x at each level to demonstrate hiding.
Use a dynamic array whose size is read at run time to compute the mean and standard deviation of n numbers.
Implement a "menu" using a switch and goto, with an out-of-range check before the jump.
Write a Collatz sequence printer using a for ... while element, then rewrite it using labels and goto. Which is clearer?