Program Structure, Blocks & Compound Statements

Any statement can be labelled: L1: x := 0.

8.1 Compound statements vs blocks

A compound statement groups statements: begin S1; S2; S3 end — it has no declarations.

A block is a compound statement with declarations at its head: begin real x; S1; S2 end.

'begin'                         'comment' outer block;
    'real' a, b;
    a := 1; b := 2;
    'begin'                     'comment' compound statement (no declarations);
        a := a + b;
        b := a - b
    'end';
    'begin'                     'comment' inner block;
        'real' t;               'comment' t exists only inside this block;
        t := a; a := b; b := t
    'end';
    outreal(1, a); outreal(1, b)
'end'

8.2 Scope and lifetime

An identifier declared in a block is local to it; it is non-local (global) to blocks nested inside.

Inner declarations hide outer ones with the same name.

Local variables come into existence on block entry and disappear on block exit (except own variables, §10.4). Their values are undefined on entry.

'begin'
    'integer' x;
    x := 1;
    'begin'
        'integer' x;            'comment' hides the outer x;
        x := 2;
        outinteger(1, x)        'comment' prints 2;
    'end';
    outinteger(1, x)            'comment' prints 1;
'end'

8.3 Statements in ALGOL 60

x:= y + 1; multiple: a:= b:= 0 (nothing) — e.g. L: end if x > 0 then y:= 1 else y:= -1

for i:= 1 step 1 until n do s:= s + a[i] begin... end begin real t;... end

Any statement can be labelled: L1: x := 0.

8.4 Multiple assignment

In ALGOL 60 all left-part variables must have the same type (Modified Report: the same type, or real/integer combinations are restricted). Subscript expressions on the left are evaluated first, left to right, then the right-hand expression.

a := b := c := 0;      'comment' all three set to 0;

8.5 The program as a block

An ALGOL 60 program is simply a block or compound statement. Standard procedures (sin, sqrt, outreal, ...) are considered declared in an invisible environmental block surrounding the program — the Modified Report makes this precise.