One of the more obscure features of Algol, which may be a bit strange to the modern beholder (and which is also to be observed in the Ackermann program, namely in the assignment part of the procedure “ackermann”), is that if-then-else constructs may not only appear as conditional clauses, but also appear in expressions.
E.g., in an arithmetic expression:
<arithmetic expression> ::= <simple arithmetic expression> |
<if clause> <simple arithmetic expression>
else <arithmetic expression>
Here an example from the Revised Report, an expression which may be used as the right-hand side of an assignment:
if a<0 then A/B else if b=0 then B/A else z
It’s like ternary expressions on steroids.
x := if a<0 then A/B else if b=0 then B/A else z
This works also with Boolean expressions:
<Boolean expression> ::= <simple Boolean> | <if clause>
<simple Boolean> else <Boolean expression>
And another, rather intimidating example from the Revised Report (also illustrating how this works as an expression):
if if if a then b else c then d else f then g else h < k
This also works with goto, which jumps to a label (designational expression):
<designational expression> ::= <simple designational expression> |
<if clause> <simple designational expression>
else <designational expression>
p.e. (again from the Revised Report)
Town [if y<0 then N else N+1]
if Ab<c then 17 else q[if w ≤ 0 then 2 else n]
to be used with goto:
<go to statement> ::= goto <designational expression>
p.e.
goto if Ab<c then 17 else q [if w<0 then 2 else n]
What about that index in conjunction with a label, as in “q[n]”, you may ask? Like in:
goto Town [n+1]
Well, this is a switch, declared in a switch statement and used to determine the effective label by index from the list provided in the declaration:
switch S:=S1,S2,Q[m], if v>-5 then S3 else S4
switch Q:=p1,w
BTW, labels are also valid arguments and may be passed to procedures.
Fun fact: Speaking of Boolean expressions, besides the usual Boolean operators, there’s also implication (⊃):
a false false true true
b false true false true
-----------------------------
a ⊃ b true true false true
So much for today’s episode of “Fun with Algol 60”…