Types, Declarations & Expressions
There is no character or string type for variables in ALGOL 60: strings may only appear as actual parameters (typically to output procedures).
9.1 Simple types
whole numbers (range implementation-defined; maxint in the Modified Report)
3.14, .5, 2.0₁₀-3 (MARST: 2.0#-3), ₁₀6 true, false
There is no character or string type for variables in ALGOL 60: strings may only appear as actual parameters (typically to output procedures).
9.2 Declarations
Declarations must precede statements in a block. There are no initializers — assign values in statements.
'integer' i, j, count;
'real' x, y, total;
'boolean' found, done;
'real' 'array' a[1:100];
'integer' 'array' grid[0:9, 0:9];
'array' b[1:n]; 'comment' "array" alone means real array;
'own' 'integer' calls;
'procedure' p; ... ;
'switch' s := L1, L2;9.3 Identifiers
Letters followed by letters or digits; no underscores; case-sensitivity is implementation-defined (MARST is case-sensitive for identifiers). Spaces have no significance, so max value and maxvalue are the *same* identifier in the reference language (in MARST, spaces outside strings are ignored too).
9.4 Arithmetic expressions
Operators and precedence (highest first):
↑ (exponentiation, left-associative: 2↑3↑2 = (2↑3)↑2 = 64) + - (binary and unary) Types of results:
+ - × of two integers gives an integer; otherwise real.
/ always gives real: 7/2 = 3.5.
÷ is defined only for integers: 7 ÷ 2 = 3, -7 ÷ 2 = -3 (truncation toward zero).
i ↑ j with integers: integer if j ≥ 0 (with special rules for 0↑0, undefined); the Report gives detailed rules for real and negative exponents.
There is no mod operator in ALGOL 60: write a - b × (a ÷ b).
Mixed integer and real operands are allowed; assignment of a real to an integer variable rounds (Report: entier(E + 0.5)).
'begin'
'integer' a, b, q, r;
'real' x;
a := 17; b := 5;
q := a % b; 'comment' 3 (integer division);
r := a - b * (a % b); 'comment' 2 (remainder);
x := a / b; 'comment' 3.4 (always real);
outinteger(1, q); outinteger(1, r); outreal(1, x)
'end'9.5 Relational and Boolean expressions
Relations: < ≤ = ≥ > ≠ (MARST: < <= = >= > !=). Boolean operators, highest precedence first:
arithmetic, then relations ALGOL 60 does not specify short-circuit evaluation; both operands may be evaluated (implementations vary). Use nested conditionals when the second operand must not be evaluated:
'boolean' ok, inrange;
inrange := x >= 0 'and' x <= 100;
ok := 'not' inrange 'or' (y != 0);'if' i <= n 'then' ('if' a[i] > 0 'then' ... ) 'comment' safe;
found := 'if' i > n 'then' 'false' 'else' a[i] = key; 'comment' conditional expression guards a[i];9.6 Conditional expressions
Expressions may be conditional (McCarthy's contribution):
The else part is mandatory in a conditional expression.
max := 'if' a > b 'then' a 'else' b;
sign := 'if' x > 0 'then' 1 'else' 'if' x < 0 'then' -1 'else' 0;