Basic algol

226 64 7MB

English Pages [132]

Report DMCA / Copyright

DOWNLOAD FILE

Polecaj historie

Basic algol

Citation preview

oe BY WR BRODERICK&PJ BARKER

A COMPUTER WEEKLY HANDBOOK

BASIC ALGOL BY W R BRODERICK & PJ BARKER

A COMPUTER WEEKLY HANDBOOK

First Published in 1967 by Iliffe Technical Publications

Ltd. Dorset House, Stamford St.

London S.E.1.

Tel. 01/928 3333

Printed in Great Britain by UNEOPRINT and offset at The Gresham

Unwin Brothers

Surrey, England

Press,

Ltd., Old Woking,

(U 2745)

© Iliffe Technical Publications Ltd. 1967

Contents Page Nos. SECTION

SECTION

SECTION

1.1.

Binary Representation

1

1.2

Elements

of a Computer

2

1.3

Input and Output Devices

4

1.4

Programming

6

2.1

A Simple Program

2.2

Numbers, Identifiers and Declarations

10

2.3

Arithmetic

12

2.4

Assignment Statement

14

2.5

Simple Input/Output Operations

14

2.6

Go to and Conditional Statements

15

2.7

Compound

Statements

17

2.8

Output of Descriptive Matter

19

2.9

Expressions ina Print List

days

3.1

Simple for Statement

24

3.2

Output Setting Procedures

26

Gea

The for=-list

27

Languages

Expressions

9

Page Nos. Flow Charts

30

The nested for-statement

31

Arrays

32

Standard Functions

35

For-while Construction

36

Blocks

40

Input and Output of Strings

45

Boolean Operations

47

Non Type Procedures

49

Type Procedures

02

SECTION

Case-study

06

SECTION

Problems/Answers

65

SECTION

Notes on Program

SECTION

Layout

81

SEC TION

8

Bibliography

84

SEC TION

a

Index

85

Report on Algol 60

87

APPENDIX

Authors’ Preface

The use of computers was once the sole province of research scientists and mathematicians but today the situation has changed and a far wider spectrum of people are using computers every day, and in the future, with the change in pattern of computers towards multi-access systems, this trend will be even more marked. To be a computer not need to have a matics or even to You do need to be and logically.

programmer you do degree in mathebe a mathematician. able to think clearly

Using the Royal Liberty School Computer, we have boys and girls of all ranges of ability, from secondary modern schools and grammar schools and all ranges of ages from 11 to 19, programming in Algol, and in general making a very good job of this. To them Algol is no longer a second foreign language, but a concise, logical way of communicating their thoughts to both their fellows and the computer.

The purpose of this book is to provide

an inexoensive Algol handbook. It is based on a series of 30 articles first published in Computer Weekly. We concentrated in general on non-mathematical problems because it was our wish that these articles should be readable to both mathematicians and non-mathematicians alike. This concept has been carried through into the book and a number of essentially nonmathematical problems have been included in the exercises at the end.

This is a handbook and while in the text we have, in general, used the subset of Algol on the Elliott 903 Coma

puter, including the Elliott input/output statements, we have mentioned the full details of unrestricted Algol in many places. The Revised Algol 60 Report has been included because it has been found useful by many of our students in clarifying the text of various books on the Algol language and for checking whether certain rather obscure constructions are valid. Because the full generality of Algol 60 is rarely, if ever, implemented and because Algol 60 does not specify any input and output statements, the reader using a particular computer configuration should at all times check the manufacturer’s manual on the particular implementation he

is using and study both the input/output instructions and the restrictions imposed which may be more or less severe than those imposed in Elliott 903 Algol.

Finally, we would like to acknowledge the help we have received from our students and from readers of the original articles in suggesting improvements to the text and from those responsible for the typing of the original manuscript. We would also like to acknowledge the help of Tony Higgins and Jim Bonnett of Computer Weekly. W.R. Broderick P.J. Barker

The Royal Liberty School Computer Department, Hare Hall, Romford, Essex.

MAY

1967

Digitized by the Internet Archive in 2021 with funding from Kahle/Austin Foundation

https://archive.org/details/basicalgolo000unse

Section

1

An Introduction to Computers

The aim of this first section is to give the reader an introduction to how computers work. As this is a book on ALGOL the treatment is necessarily brief and for a more detailed study the reader is referred to one of the books on this topic in the bibliography.

Section

1.1

Binary representation

The computer is often described as an electronic brain but it would be more appropriate to describe it as a high-speed moron. These machines are surrounded by an unjustified aura or mystique which has its origins in the speed of computers and the complexity of the work undertaken by them.

Like all really brilliant inventions the computer is intrinsically a simple device; the scope of the work it can undertake is usually limited only by the ingenuity of the people using it. Information is transmitted between the various parts of a computer as electrical pulses. It is simpler and cheaper to detect the presence or absence rather than the magnitude of such pulses. Therefore if notationally we use a 0 to represent the absence of a pulse and 1 to represent its presence, then we can build up a counting system based on 1s and Os for our computer arithmetic.

One sent give mal

possibility is that we could use a string of 20 pulses, for instance, to reprethe number 20, but this is too cumbersome and slow. It is far better to the 1s and Os positional significance in the same way as we do with decinumbers.

It is not intended here to give more than a very basic description of binary arithmetic and its implications for computers, but in view of the fact that virtually all electronic digital computers are based on this principle, a very simple introduction is called for.

Consider the number

tet 000.453

1326—this means, as we all know:

100 eke2- x 10. +:6-% 1

or less familiarly

1 =< 103 + 3.x 102"+'2 x 101+ 6 x 10° (Note: any number to the power ° = 1, i.e., 10° = 1, 3° = 1, 2° = 1, etc.) Thus in the decimal numbers.

scale we use 10 as our radix and the figures 0-9 as our

In the binary scale we use 2 as our radix and 0 and 1 as our numbers.

The relationship between binary and decimal numbers can be obtained by expressing a binary number in terms of powers of its radix.

Consider the binary number

110110

This means

93 +1

224+ 1x

56:8 Fol SoA

1

2140 2

x 20

Oral

+0

Example

Binary

Hon 1011+ 11000

Decimal

1s 11+ 24

Similar rules can be deduced for subtraction. Furthermore multiplication and division can be treated as repeated addition and subtraction respectively, although other methods are almost always used in practice. Finally, the reader may like to try the following example of binary addition.

10010 + 11010 = ? Check your answer by converting all the binary numbers to decimal.

Section

1.22

Elements

of a computer

A particular computer will be designed to deal with binary patterns of a certain size. The number of binary digits (bits) in this basic pattern, which is known as a word, varies from one machine to another, depending on the type of work for which it is designed. Commonly, word lengths may be from 15 to 50 bits. The maximum decimal numbers computers can deal with in one word will therefore range from 16, 383 to more than 281 million million. There is, however, a rather more flexible system based on smaller groups normally between four and eight bits. One of these groups contains a single character, either a decimal digit or a letter, and the machine is designed to deal with these in packets of varying size depending on the information being processed.

We shall deal only with computers

having a fixed word length.

The accompanying schematic block diagram of a computer plicated at first sight, however let us examine it in detail.

(Fig. 1) looks com-

The store: This is a device for storing information and making it available to the remainder

of the computer very quickly—in some

cases as quickly as

1 millionth of a second (1 microsecond). Numbers and instructions are all stored as binary patterns in the store, and care must be taken not to treat a number as an instruction, or vice versa.

2

ARITHMETIC UNIT

NAA

CONTROL UNIT

OUTPUT UNIT

Y

fCe STORE

Fig.1.

Schematic diagram of a computer

For ease of reference the store is divided up into words, and each word is given an address, for simplicity this address is usually a number.

Thus in a store with 8, 192 words the words will be numbered 0, 1, 2, 3 and so on to 8,191. This means that the numbers or instructions stored in any particular location can easily be referred to. The arithmetic unit: This is the part of the computer where all the arithmetic operations are carried out. The arithmetic unit contains a storage element called the accumulator. The results of all arithmetic operations are left in the accumulator when the operations have been completed.

The control unit: This unit comprises two main sections, the decoder, and the sequence control register (SCR). The sequence control register contains the address from which the next instruction is to be taken. Normally instructions are taken in sequence from the store and decoded. However, as mentioned previously, the computer has the power to make simple decisions, usually three. They are: Is a number in the accumulator

0 (greater than zero) 0 (less than zero) NSN 0 (equal to zero)

and if the answer to the question is Yes then the next instruction to be obeyed will not be the next in sequence in the program, but one specified by the programmer. If the answer is No then the next instruction in sequence is obeyed. The jumping out of sequence is effected simply by altering the SCR. The store, the control unit and the arithmetic unit are often grouped together as the central pro-

cessing unit (CPU).

Section

1.3

Input and

output devices

INPUT UNIT. A variety of input units are available for computers, some read cards, others punched paper tape, but basically the principles of how these devices work are the same, and we will consider paper tape. Each row, of holes across the tape (See Fig. 2) represents a character, each character having its own unique pattern. (Note, if a hole is considered as 1 and no hole as 0 we have a binary pattern again.)

SPROCKET Fig.2.

HOLES

ONE

CHARACTER

Paper tape showing the characters in eight unit code

A beam of light is focused on the tape in the tape reader, and if a hole is present it shines through and onto a photo-electric cell producing an electric current, which is then fed into the computer. There is in fact a row of nine cells across the reader, one for each of the eight information holes, and one for the row of small holes (called the sprocket holes).

Output unit. A wide variety of output devices is available, either for direct printing of results (on-line printing) or producing coded information which is later fed to a printing device (off-line printing). On-line printers may be teletypewriters, or high speed line printers. The former are only suitable where the volume of output is small as they operate at speeds of between 6-15 characters per second.

For data processing a line printer capable of printing up to 1, 000 or more lines of up to 120 characters per minute is the most likely choice. The medium on which information may be output for subsequent off-line printing may be punched cards, magnetic tape, or punched paper tape. The latter is normally used only with teleprinters but line printers may be operated from cards or magnetic tape. We shall consider only the punched paper tape system

in this book.

Backing store. The high speed store mentioned earlier in connection with the basic computer is very often not large enough to hold the amount of data to be processed, and in addition storage space taken up by the program may be quite large. To allow for this, extra storage, known as backing store, may be attached to the computer.

These stores are normally magnetic tape, magnetic discs, magnetic magnetically coated plastic cards. None of these media can present mation to the central processor as fast as the basic computer store program must provide for blocks of information to be passed to the store for processing.

4

drum, or inforand the high speed

To find a particular piece of information on, for instance, a reel of tape, may well take several seconds, which is a long time to a computer. It is important ae ad to organise the transfer of information to cause as little delay as possible.

Fig. 3.

The Westrex ASR 33 teletypewriter is typical of the keyboard devices used with a computer

The teletypewriter. How is the paper tape prepared for the computer? done on a teletypewriter, one form of which is shown in Fig 3.

This is

Basically this machine consists of a keyboard almost identical to that of a typewriter, a punch unit being connected to the keyboard so that when a key is depressed the corresponding pattern of holes is punched in tape at the same time as the character is printed. A mechanical reader is also fitted so that the tape can be read and a print out obtained of the tape. The punch can be switched on to copy all or part of the tape being read.

Printer* |Punch

1

v

| Reader |Comment

N

For producing tapes for input to computer.

2

J

v

For printing up computer output.

3

J

J

J

For copying tapes and when combined with 1 and 2 for editing them.

* On most machines the printing unit works all the time providing a permanent record of all operations.

Fig.4.

Section

The functions of a teletypewriter. These may be combined to enable a wide variety of work to be done. The punch may be switched on or off to copy all or part of a tape being read.

1.4

Programming

languages

PROGRAMMING. There are many ways in which programs can be presented to computers, but ultimately all their functions are carried out on binary patterns. It would be highly inconvenient actually to prepare programs in binary and this is virtually never done. The most basic form of program is one written for a particular machine in its own machine code. Here the instructions and store addresses are usually written as a series of decimal numbers, a punched paper tape being produced by a punch operator copy typing this on a teletypewriter.

A program will be provided by the computer manufacturer which will translate the decimal numbers into binary and store them in the desired locations. This translation program will be punched in binary and entered into the machine by means of a set of instructions called the initial instructions which are permanently held in a special part of the store where they cannot be erased.

The initial instructions can only deal with binary coded programs, but without them no information could be put into the computer.

We shall give an example of a machine code program as written for the Elliott 903 computer, which has a particularly simple machine code. Each instruction consists of an order code number and a store address. In addition an oblique stroke in front of the order number will cause the address number to be increased by the contents of storage location number 1, which is reserved for this purpose. A selection from the order code of the Elliott 903 computer

6

is given below to

illustrate the scope of operations that can be performed, is referred to as N, and its contents as c(N):-

The address number

Order No.

1 2

Function

Add c(N) to accumulator. Negate c(accumulator) and add c(N), i.e. form c(N)—c(accumulator) in accumulator. Copy c(N) into accumulator

from the store.

Copy c(accumulator) into c(N.). Jump if c(accumulator) is zero to order which is c(N). Jump unconditionally to c(N). Jump if c(accumulator) is negative to c(N). 1 Multiply c(accumulator) by c(N). 1 wWwnNOaoONT Divide c(accumulator) by c(N). As an example, suppose that location 1000 contains a man’s basic salary for one month, location 1001 contains extra gross pay for overtime, and location 1002 contains total deductions. To obtain the net payment we should proceed as follows:Function Address 4 1002 2 1000 1 1001 If the answer was 5 1003

Notes

Load deductions into accumulator. Form (— deductions + basic pay) in accumulator. Add overtime to accumulator. to be stored in 1003 we should add:-

These instructions would be punched exactly as printed in the table.

Whereas programming in machine code requires great attention to detail and in addition mades the understanding of a program difficult except to the enlightened few, the use of an autocode or programming language makes the computer do much of the routine hard work, and the finished program is readable by anyone with only the most basic knowledge of the programming language. This in fact reduces programming time at the expense of using the computer less efficiently. Before deciding if the program is to be written in machine code or a high level language it is usually necessary to know the nature of the problem, how often the program is to be used, and so forth.

THE COMPILER In order that the machine may “understand” the programs written in these high level languages the computer has to be programmed to translate these

complex statements into its own machine code. This large and complex program is usually provided by the manufacturer of the computer and is called a compiler. Some computers have compilers for several different languages such as ALGOL, FORTRAN, COBOL, PL-1. (These bizarre names will be explained

later.)

ra

The relationship between compilers and computers

is shown in Figure 5.

Compilers are instructed to give indications of grammatical errors made by the programmer. They will not, however, detect logical errors made by the programmer.

PROGRAMS IN MANY LANGUAGES

, MACHINE CODE

PROGRAM

S

"|

b

ae PROGRAM

gence acd ee

COMPUTER caren

Gems Sore Sore

Gs

COBOL | OR fe |

COMPILER ‘TAPES Fig.5.

The use of compilers diagram

The problem programmed for comparison.

NETPAY:

above in machine

= GROSSPAY

This is indeed more

and machine

+ OVERTIME

codes are shown in this

code is written here in ALGOL

— DEDUCTIONS;

readable.

Four of the main high level languages are: ALGOL. ALGOrithmic language.

Language—an

COBOL. COmmon Business programming language.

FORTRAN. FORmula language.

international

scientific programming

Orientated Language, an international

TRANslator—a

commercial

widely used scientific programming

PL-1. Programming Language 1—a new language designed to be suitable for both commercial and scientific work.

Section

2

An Introduction to ALGOL It is important, when learning to program a computer, to be able to write complete programs as soon as possible. When the reader has completed this section he will have covered enough Algol to write simple programs for a variety of problems.

Section

2.1

A Simple Program

Consider this Algol program to read in two numbers one first. Example compare numbers; begin real a, b; read a, b; if a> bthen printa,b else print b,a end; data 2.8 1.935

and print out the largest

Line No. 1 2 3 4 5 6 u 8

The example above illustrates a complete Algol program albeit a simple one. The line numbers on the right-hand side do not form part of the program but are put there for reference purposes only.

The dialect of Algol which we are using for this series is that for the Elliott 903 computer. The other dialects of Algol are so similar that no further distinction will be drawn here. Line 1: This line contains the title, which is essential in Elliott 903 Algol so that the program can be identified. The title continues until the first semicolon and all programs must have a title. Line 2: begin really indicates that the actual program starts here. real tells the compiler that the numbers which we are going to refer to are likely to have decimal values and fractional parts, it also tells the compiler that there will be two numbers which we are going to call a and b. The word real is called a declarator, because it declares to the compiler the type of numbers we intend to deal with and the names we will use for them. The letters a and b

are identifiers. Line 3: read instructs the computer to read in two numbers from the data tape (when the program is being executed, rather than when it is being compiled) and put the first one into the location we have called a and the second one into the location we have called b. read is said to be an input statement.

Line 4: The sign > means “greater than”, therefore if this is true (and it will be with our data) we will print the numbers a and b in that order. Line 5: else can be best translated as “otherwise”. In other words had a been 1.6 and b been 2.192 the computer would have printed b then a in that order. print is said to be an output statement.

Line 6: This end tells the computer that we have now finished this program. The semi-colon after this final end is not required in ALGOL 60. We will continue to follow the Elliott 903 ALGOL convention and insert it. Lines 7 and 8: These lines contain the data, that is the numbers to be read in by the program in the order in which they are required. The data could be on a separate piece of tape, and the word data could be omitted. Finally it must be emphasised that there are two distinct operations to be performed before an Algol program gives out answers.

(1) it must be compiled, then (2) it must be executed.

‘.

EXAMPLE COMPARE NUMBERS; “BEGIN” “REAL” A, B; “READ” A, B; “IF” A > B “THEN” “PRINT” A, B “ELSE” “PRINT” B, A “END”; DATA 2.8 1.935 Fig.6.

oi

The program as it is punched for input into the computer

Output

Comments

EXAMPL

Information

EXAMPL4798 QACODL 4830 QAVNDA 4833

how and where it has stored our program -—this need not concern us at this stage, except that FIRST is the address of the

FIRST NEXT 4798 4838 EXAMPL 2. 800000 1. 935000

first instruction in our program, and NEXT the first free store location.

Fig.7.

Section

given by the compiler

about

ANSWERS

How the computer output will be printed

2.2

Numbers,

Identifiers

and

Declarations Numbers in Algol: In Algol a distinction is made between integers (whole numbers, i.e. without fractional parts) and other numbers which are referred to as real.

The most important distinction between real numbers and integers in Algol is that integers are held with complete accuracy, whereas real numbers subject to rounding-off errors.

10

are

*

It is also important to note that real numbers take up more storage space than integers in most systems, and when large amounts of data are being held it is inefficient to hold integers as reals. In addition arithmetic is usually carried out much faster on integers than on real numbers.

Integers must be written in the usual way without a decimal point. and —7950 would be acceptable but not 27. or 27.0

Thus 275

Real numbers may be punched in various ways.:A real number which happens to be an integer may be punched as such, but will be stored in the same way

_as other real numbers if assigned to a real identifier. For those whose mathematics are rusty it should be sufficient to note that real numbers may be punched in their normal decimal point form, as in the first example below. The other alternatives are not essential. Real numbers may also be punched as a number, including decimal point if desired, multiplied by a power of 10. For this purpose a type key is provided with a subscript 10, and a number such as 37. 954 could be punched as:

37. 954 3.7954,ol 379. 54,5—1 . 37954, +02

3

A sign may be used, but it is not necessary before a positive number. The subscript 10 must not follow immediately after the decimal point. Thus 56.,)—1 is inadmissible, and would have to be punched as 56.0,,—1 or 5.6, etc. Moreover only integral powers of 10 are allowed so that 62,,1.5 would be inadmis-

sible.

2

Numbers punched according to these rules may be used in programs data tapes.

or on

On output real numbers are usually punched with a decimal point in the appropriate place, e.g. 16. 71934. An integer which had been assigned to a real variable might be printed as, say 24. 00000.

The international language Algol 60 does not define input and output operations, since these depend on the design of the computer and its peripheral equipment. A particular version of Algol will usually provide good layout and no more digits than are required by the programmer. Identifiers: Every storage location which has to be referred to in a program

must be given a name (or identifier) by the programmer. The choice is almost unlimited, and although we used a and b in our first example we could have used much larger combinations of letters. In fact an identifier can consist of nearly any combination of the letters a to z and the digits 0 to 9, with the reservation that the first character must be a letter.

Possible identifiers would be: x, alpha2, rhubarb but not: 2nd, 2henry4. Many compilers examine only a certain number of characters of an identifier, and in fact there will always be some limitation. In many cases only the first six are examined, and therefore identifiers such as incomel, income2

cannot be distinguished. Usually this confusion would cause an error indication to be given, but with care this can be avoided.

11

Certain names cannot usually be used because they are identifiers for standard functions available in Algol (e.g. sqrt for square root). These functions will be described in a later Section. Declarations: All identifiers must be declared before they can be referred to in program statements, and for the time being this means that they should be declared at the beginning of the program. When a number of identifiers of the same type are declared they are listed with commas separating them, and the list is terminated by a semi-colon. Thus the beginning of a program might be: title; begin integer n,r, count; real x, y; It does not matter in which order the identifiers are listed or which type of variable is declared first and more than one declaration of the same type may be made.

e.g. begin integer a, b; real x; real y, z; integer c; etc.

Section

2.3

Arithmetic

Expressions

Arithmetic expressions: Identifiers can be combined with the arithmetic operators, +,— etc, and parentheses, ( ), to make arithmetic expressions. Generally these operators are the same as those met at school, but they will be defined here for completeness.

()

Parentheses—the function of these is to enable certain operations to be performed before others. In this case, as in algebra, the expression in brackets is evaluated before the remainder of the expression.

?

Exponentiation (or raising to a power). x? is written x7? 2. This is because there is only one line available for program and one size of type available on most teleprinters. (The exception is subscript 10.)

x

Multiplication (this must always be written and never implied). In Algol 60 the multiplication sign is x. In manuscript it is often advantageous to write the multiplication sign as an asterisk to avoid confusion with the letter x.

/

Division. Straight forward division of any combination of integer or real numerator or denominator giving a real result.

+

Integer division only defined when numerator and denominator are both integer. The result is the greatest whole number less than or equal to the quotient. This whole number is determined as if all the signs were positive.

Examples: 52h a2 Ny SS 10/4 = 3.3333 + Addition — Subtraction

12

LOK 3 a3 —1/4 =—1.75 ll

All expressions are evaluated from left to right, subject to the following restrictions:

(a) (b)

expressions in parentheses are evaluated first. arithmetic operations have the following hierarchy: T exponentiation

followed by x multiplication \ / division These operations are of the same + integer division)

level in the hierarchy

followed by

+ addition i ees An example

: These operations are of the same

3 level in the hierarchy

of this hierarchy is shown in Fig 8.

CONSIDER

THE

EXPRESSION:

CAS + TRAF 4 (CHAOS — POLI) / ACCID + PEDES It is evaluated as follows: CAS + TRAF A (CHAOS — POLI) / ACCID + PEDES

Fig. 8.

Now try these simple questions:

(a) is MORE

x (A + B) the same as MORE

X A+

B?

(b) is ACCN / (TIM x VEL) the same as ACCN / TIM Xx VEL? (c) isA + (B —C) the same as The answer

A+ B—C?

is, of course, No for a and b, but Yes for c.

(a) let MOR

(b) let ACCN= 6 TIM =3 VEL =2 ACCN / (TIM x VEL) = 6 / (3 x 2)=6/6 (ACCN / TIM) X VEL = (6 / 3) xX2=2x 2 13

(gee ape Oe Meat 3—4=5-4=1

Section

2.4

Assignment

Assignment statements:

Statements

The most common

form of the ALGOL

assignment

statement (which assigns a value to a variable) is variable := expression.

This means that the expression on the right hand side is evaluated and its value is assigned to the variable on the left hand side. There is a very real difference between the sign = and the symbol : =. The former expresses a relationship (i.e.a = b); from this we can deduce that b = a. But := is best interpreted as “is replaced by” or “becomes” as in the example.

A:=b+c with b = 2 and c = 4, A becomes 6, any previous had is “rubbed out” by assigning it a new value. Therefore is nonsense, A := A + 1 makes very good sense, and means replace the old value of A by one more than itself. Clearly to write b +c :=A.

value A while increase it is not

may have A=A+1 A by 1 or allowed

The formula for simple interest is

I

Beak 100

Translated into ALGOL

this is

I:= P x T x R/100;

The formula for the yield of an investment is yield =

annual income

X 100

cost price

In ALGOL

this is YLD := ANINC

x 100/CSTPCE;

Notice the semi-colon at the end of each assignment statement. The purpose of this is to tell the computer that this is the end of the statement. The basic word end also has this function.

Multiple assignment statements are also valid; i.e.a:= b:= c:= d:= x12 + y12; means that a, b,c and d all take the value of x? + y? and a,b, c, and d must all be of the same type, i.e. all real or all integer. It should be noted at this stage that all ALGOL statements are obeyed in the sequence in which they are written on the coding form unless specific action is taken to make the program jump out of sequence.

Section 2.5

Simple Input/Output Operations

Input/output statements. As mentioned previously the international definition of ALGOL does not specify any input or output statements. It recognises these

14

are needed but realises that their form will depend on the configuration of the computer

available.

We intend to follow the Elliott input/output system here and the two main input /output statements are (i) read (ii) print where is in fact a list of variable names or expressions. It is possible to include other items in lists but these will be ignored for the time being.

read a, b, tax, l, s, d; has the effect of reading the next six numbers from the data tape and assigning the first to a, the second to b, the third to tax, etc, so that if the data tape contained the numbers 6, 10. 3, 8. 459, 2, 6, 3 in that order the values of a, b, tax, 1, s,d after the read statement has been executed wauld be 6, 10. 3, 8. 45S, 2,6, 3 respectively.

if a=

5.632;b=— 10,z = 2.0 print a, b, z72 + 5, 51;

would cause the following printing to take place:

5. 632000 10 9. 000000 51 Note that all the variables are printed on separate lines.

The variable b must have been declared as an integer and z as a real number; a is of course real.

Section

2.6

Goto and Conditional

Statements

Jumping out of sequence. Normally instructions to the computer are obeyed in the order in which they are written; however, one of the most important facilities of an electronic computer is its ability to leave one sequence of instructions and jump to another. This will most often be done after comparing the results of a calculation with some predetermined criterion. The basic programming element involved is the goto instruction. (The words go and to may be separated with a space if desired, but they are treated as a single word by the compiler.) Following goto there must be an identifier called a label, which is attached to the instruction to which the jump is to be made.

Thus an instruction to jump back and read more data might look as follows: next: read x;

goto next;

Notice that the label is followed by a colon, and then the order which is labelled.

15

In ALGOL 60 which we are switches and and therefore

it is not necessary to declare labels, but in the Elliott 903 version considering this must always be done. Labels are declared as a switch list of one or more labels must be given an identifier looks rather like an assignment statement.

Thus we might have the declaration: switch ss := next, more, again, finish;

It is usual to use a string of s’s as the switch list identifier, and at least two should be used so as to avoid clashing with a possible variable s. finish may at first seem a curious label, but the end of a program can in fact be labelled so that some instructions are omitted. Thus:

goto finish; these statements

are skipped. print y; finish: end;

| =
0 then goto next; The word if must always be followed by a relation and that in turn followed by the word then which must be followed by a statement, which need not necessarily be a goto statement. Moreover the relation could involve quite complicated arithmetic expressions. e.g.

ifx + y=zT

2 + 2 then z := 4;

The relational expression will usually involve one of the following symbols:

> greater than > greater than or equal to = equals

< less than or equal to < less than ~ not equal to

Suppose we wish to read in 100 numbers Our program might be as follows: Example Number

from a data tape and print their sum.

Summation;

begin integer r; real a, sum; switch ss :=another; ca 0 Sui. 40:

another: read a; sum := sum +t a; pe Geese 18 if r < 100 then goto another; print sum

end;

The general effect of the if statement is that if the relation is true the statement following then will be executed, otherwise it will be ignored.

Conditional statement.

16

Where there are two alternatives, one of which is to be

carried out if the relation is true and the other one if it is false, we can introduce the word else. Thus for example:

if x = 0 then y:= 1 else y:= 2;

if x is not equal to zero then y becomes y becomes 1.

2 otherwise

(i.e. if x is equal to zero)

Note that there is no semi-colon before else which cannot stand on its own, but must be part of the statement following then. In general terms

the conditional

statement can be written as:

if R then S, else S,; S33

Where R is a relation and S,,S, and S, are statements. If R is true the statements S, and S, are obeyed in that order. If R is false the statements S, and S., are obeyed in that order. It should be pointed out that if cannot follow then. if x = 1 then if y = z then m: =

Consider the statement,

1 else m := -z;

this is ambiguous because it is not clear to which then the else corresponds. Algol does not permit ambiguities. This restriction can be overcome by using the statement brackets begin and end as follows. See Section 2. 7.

(a) if x = 1 then begin if y = z then m: = 1 end else m: = —z; (b)

if x = 1 then begin if y = z then m: = 1 else m: = —z; end:

Conditional Assignments: An assignment clause following the assignment symbol.

statement may contain a conditional

e.g. x:= if a < 0 then 0 else 1; This has the same

effect as:-

if a < 0 then x: =0 else x:=1; When written as a conditional assignment some value must be assigned to x so that we must have a clause of the form. x := if < relation > then < expression > else < expression >

Conditional Goto: In the same

way a goto statement may be made conditional

e.g.goto if a + 1 > C then more else less;

More and less will of course be labels. This construction is not allowed in Elliott 903 Algol.

Section

2.7

Compound

Statements

Compound statements. It was explained in section 2.6 that a complete if statement is of the form: if relation then statement. It often happens that as a result of the relation being true we wish to execute a series of instructions. Suppose we have: if x ~0 then y :=x ee

+ 4; Dae i

As it stands the last statement

would be executed whether

or not the relation

D7,

was true. In order to bring it under the control of the if statement the two statements following then must be enclosed in statement brackets. These brackets are in fact the words begin and end which we have met already as indicators of the start and finish of a program. In fact the whole program is enclosed in statement brackets and forms an entity similar in many respects to a compound statement. In the example

shown above we should write:

if x ~0 then beginy := x + 4; eRe ar A end;

Note that there must never be a semicolon after end when it is followed by else.

e.g. if x ~0 then begin y := x + 4; po Ree 0 Sr 1 end else begin y := 0; ip Ses i end; Statement brackets may in fact be nested one inside the other in the same way as ordinary brackets in an arithmetic expression. Note that following begin the program is inset to the right so that the corresponding end can be written underneath without anything between. This makes

it easier to check in a complex program

that the number

corresponds to the number of ends. If there is a different number and ends in a program the compiler will not accept it.

of begins

of begins

Let us consider now a program to read in a number the equivalent in pounds, shillings and pence.

of pence and print out

Example, money; begin integer, 1, s, d; read’ d; t= s):= 0:

1 2 3

print d;

4

if d > 240 then begin 1 :=d + 240; d :=d—240 x 1

5 6

end;

if d > 12 then begin S:=d+12; d:=d—s x 12 end; print 1, sameline s, d end;

8 9 10 nal 23

As before the figures on the right are not part of the program but merely number the lines. Line 3: We must make 1 and s zero to start with, otherwise say, 10 pence only, no value would be assigned to 1 and s.

if our data were

Line 4: Printing the data is often required to make the output complete in itself. Line 6: The result of the division is an integer and we do not know the remainder. This instruction reconstructs the remaining pence.

18

Line 11: In Elliott Algol the word “sameline” ensures that all items coming after it in the print list, are printed on the same line. If the number

257 1

read in was 257 the output from this program would be:

1

5

This output contains no words to say what the data represents. In section 2. 8 we shall explain how words and headings may be printed so as to make the output intelligible. In the meantime modifications:

readers might like to rewrite this program with the following

(a) introduce a switch so that the program negative number of pence is read.

(b)

continues to be executed until a

eliminate the if statements, andl :=s:=

Note that in the statement a := b +c; the value zero.

Section

2.8

0.

if b is less than c then a will be assigned

Output of Descriptive

Matter

There is no unique program for any problem. There could be many variations of the solution, given below, to the problem discussed in section 2. 7. Line No

Example money modified; begin integer 1, s, d; switch sss := terminate, more; more: read d;

if d < 0 then goto terminate; print d;

|

1 s= d = 240; d:=d— 240 x1; Sid 12: ade=d—12 x's; print “L”, ‘£’, sameline, 1, s, ‘s’, d, ‘d’; goto more; terminate: print “L’, ‘end of program’ '

— —RDMNFHWNHe COMA KH

_ i)

end;

In essence this program is very Similar to that given in Section 2.7. differences are explained below.

The

Line No 3.

The two labels required for this program

have been declared here.

5.

This is the statement that examines the value of d, and if it is negative then causes the computer to skip the calculation and finish the program.

7 and 9. Because a +b is zero when b> a the use of the fact makes the zeroising of 1; s, and d redundant.

11.

+ operator here in

This print list contains some new elements, namely those (string quotes). Each of these elements is printed exactly Additionally an L between “and” has special significance, the characters that cause the teleprinter to start printing

between ‘and? as it is written. it means punch on a new line.

19

12.

Theabove means that this statement will cause the words “endof program” to be printed on a new line.

For a data tape containing the numbers

230, 750, —1, the output would be as follows:

230 oe

0

19s

2d

33

PAY

(atl

750 :

end of program.

Printing descriptive information. In any ALGOL system methods of printing descriptive matter, as well as figures, must be made available. No computer output is really complete without some such descriptive printing, and therefore, although methods vary from one machine to another, we shall describe the system used with the Elliott 903 computer.

In an Elliott 903 ALGOL program the items following the word print comprise a “print list”. The elements ofa print list can comprise variables, expressions, procedures and strings. It is the outputting of strings we plan to deal with here. As stated briefly above, anything between single string quotes ‘and’ will be printed as it is written. Thus: print

‘this is an example’,

will cause the words

this is an example to be printed. It should be noted that no new line character nor spaces will be punched before the first letter. Consider

the print statement:

print tax,

‘income tax’, sup, ‘superannuation’,

If tax and sup were integers with values of this statement will be to print

19 and 6 respectively, then the effect

19income tax 6superannuation Note that each of the elements of the print list is separated from the next by a comma. To enable us to program an orderly presentation of results, facilities have been provided to output spaces, new lines, etc.

To obtain these facilities it is necessary to write certain characters within double (or inner) string quotes. For instance, “S” will cause a space to be punched. Therefore print tax, “S’, ‘income tax’, sup, “S”, ‘superannuation’,

will cause

19 income tax 6 superannuation to be printed.

20

If six spaces were required then “S” would have to be replaced by “S6”. Similarly, “L8” will cause eight lines to be skipped. Just to make sure that the reader understands the use of string quotes he should write a print statement to output the following pattern:

+

+

+

amOMgshe

+

+

ls

Ost.

>a nla. ee We i

SSO

+

ot i

OE

+

+

+

a

ab

+

making maximum

use of inner string quote facilities.

sents a space.

The symbol | | repre-

The solution is given below. It should be noted that when writing out such expressions the line should always be broken if necessary immediately after a comma and at no other place.

Print es os

eo 4Ohrar”. Soars

rt

eM

cea

10467241”,

Urges

Le

age Sn

©?

aS

Xe

O)2e sy

2

Ft:

This statement will cause the correct patternto be output. Figure 9 sets out the range of characters allowed in inner string quotes and their effect.

Character in inner string quotes

Basic effect in program

Effect of letter followed by digit N

allowed | print new line allowed | print space allowed | punch blank tape}

allowed | print (quote) allowed | print (unquote)

repeat effect N times repeat effect N times repeat effect N times

repeat effect N times | repeat effect N times

allowed | punch halt code | repeat effect N times essential output binary pattern with decimal value N

Any other

Fig.9.

no

error

Inner string characters

Writing a statement like that immediately above is rather tedious. string quotes can be nested within one another.

In fact these

Therefore

print ‘it is quite simple

‘L’ well isnt it’;

will cause

it is quite simple well isnt it to be printed.

21

Finally, the answer

to the problem set earlier could have been written

print, * Go 3-++++.+E 0 (ORT SOX aa

S+-O.'S? OF

“67, ee

ii es

Notice that a comma was introduced when a new line was started. This was because otherwise the characters carriage return and line feed would occur within single or double string quotes. If in single string quotes they would merely cause a new line to be started, in double string quotes they would cause an error and the program would stop. The importance of heading and describing results cannot be over -emphasised. Computer

output which merely

consists of rows and columns

of numbers

may

be meaningful to the person who wrote the program, but it is unlikely to be intelligible to anybody else, and indeed it is unlikely to be intelligible to the author after the passage of some time.

Section

2.9

Expressions ina Print List

COMMENTS There is one more facility in ALGOL which we would like to introduce here. As mentioned earlier, ALGOL is a language often used for publishing programs. It is clearly useful to be able to write into a program some descriptive material (perhaps to describe the problem, the method of solution, or some assumption made). This facility is provided by the comment instruction, and the insertion of any material between the word comment and a semicolon will cause it to be treated only as descriptive material, and it will not be processed by the computer at all.

Example; begin integer i; comment this program prints out the numbers 1 to 10; for i:= 1 step 1 until 10 do print i

end; The word comment may only be used immediately after a semicolon, or the word begin. Comments may also be inserted between the word end and semicolon, although in this case the basic word comment is not used. Here is an example. print x end of program;

Expressions in a print list. The printing of a variable in a print list has already been examined. It is possible to write an algebraic expression in a print list. The effect of this will be to cause the expression to be evaluated and the value of the expression to be printed.

22

%

For example, if

ab c and d are real, and have the values 1.0, 2. 5,3. 5, and 2.0

print a,b, c,d, (a + b + c)/d; would have the effect of printing

1. 2. 3. 2. 3.

0000000 5000000 5000000 0000000 5000000

The following program will read in 10 pairs of numbers and print their sum, their difference, and the average of all the first numbers in the 10 pairs, and the average of all the second numbers. Example pairs; real a, b, suma, sumb; begin integer n; switch ss := next; suma := sumb := 0; fec—a0)s comment n (an integer) is not of the same type as suma and sumb, which are real, and therefore cannot be included in the multiple as-

signment statement above; print “LS4’ sum ‘S5’ difference’; comment This will cause the words “sum” and “difference”; to be printed as column headings for the output. Real numbers occupy 10 spaces on output, eight digits, decimal point and sign; next:

read a, b; print a + b, sameline, “S2”, a — b; comment Output spaced to come suma := suma + a; sumb := sumb + b; iN Sn ae ie

under

headings;

comment n is used only for counting the pairs; if n < 10 then goto next;

print “I

“L’ average of first numbers is’, sameline, suma/10,

average of second numbers

is’, sumb/10;

comment No “sameline” is required here since it already appears in the print list. We obtain the new line by asking for it specifically;

end;

“4

23

Section 3

More

Powerful facilities in Algol

The power of a computer lies in its ability to perform a series of simple operations many times over at high speed. All high level languages provide facilities for programming this process simply. In Algol this facility is called the for-statement....

Section 3.1

The Simple for statement

We concluded section 2.9 with a program to perform various operations on 10 pairs of numbers, and we used an integer n to count the pairs. Each time that the program loop was executed we had to increase n by 1 and then examine it to see if we had reached 10. It would have been much more convenient if we could have said “do the following piece of program 10 times”. ALGOL, does, in fact, provide a statement for doing this, although in a different form. We must still declare our counting variable n, and in effect we say ‘for values of n from 1 to 10 in steps of 1, do the following statement’. The statement is actually worded as follows:

for n := 1 step 1 until 10 do Following do there must be only one statement, so that if there are several instructions in the loop, we must make a compound statement using begin and end. Note that the words for, step, until, do are basic words in ALGOL, and must be underlined in manuscript or printed in bold face type.

The example

in section 2.9 could therefore be re-written as below:

Example pairs modified; begin real a, b, suma, sumb; integer n; suma := sumb := 0; print ‘‘LS4’ sum ‘S5’ difference’; comment

The loop begins;

for n := 1 step 1 until 10 do begin read a, b; print a + b, “S2”, sameline, a — b; suma := suma + a; sumb := sumb + b; comment Statements performing parallel operations may be put on the same line to emphasise their similarity. Remember statements are separated by delimiters semi-colon, end, else, etc, and therefore the semicolon is sufficient here. New line or space characters may be used as separators for data, but they have no other significance in ALGOL, as far as the execution of the program is concerned; end of loop;

print “L’ average of first numbers is’, Sameline, suma/10, “L’ average of second number is’, sumb/10

end;

The for statement is not limited merely to counting operations, and it is in fact extremely powerful because of its great flexibility.

24

The variable n, called the controlled variable, may be used in instructions in the loop. Thus we could say: for n := 1 step 1 until 100 do

begin

print n; Sl :=S1 + n; S2 := S2:+ nT 2; print sameline, $1, S2

end;

This would give us a table, for n going from 1 to 100, of the sums of the first n integers, and the sums of their squares (assuming S1 and S2 were zero before

entering the loop). The controlled variable need not go in steps of 1, nor is it restricted to positive numbers. In the Example above, any of the following would have performed our counting operation equally well:

for n := 10 step — 1 until 1 do for n:=—5 step 2 until 13 do for n:= 3 step —5 until — 42 do

(1) (2) (3)

At each entry of the loop the controlled variable is examined to see if it has reached the limit. If it is inside or equal to the limit, the loop is executed again. Thus if the controlled variable is increasing the loop will be executed if variable < limit. If the variable is decreasing the condition will be variable = limit.

This means that in (2) above, the limit could be 14, and in (3) it could be —46 without affecting the number of loops performed. The for statement is not restricted to constants for specifying the way that the controlled variable alters. The initial value, together with the step and limit may all be variables, so that we could have:

for n := i step j until k do Both j and k can be varied during the execution of the loop, so that n would vary by a different amount, and be compared with a different limit at each execution of the loop. Consider the following: ee eleSuines 10. for n := 1 step j until 50 do begin print j, sameline, n, sum; sum := sum + n;

j:=jt+2 end;

At the start of successive loops the values of the variables are as follows: j

1 3 5 a 9 11 13

n

iI 4 9 16 25 36 49

sum

0 1 5 14 30 55 91

After the next step n has increased beyond 50 so that the looping stops.

The

values of n are the squares of 1, 2, 3, etc, so that at the end, “sum” contains the sum of the squares of the first seven integers. 25

The variables involved in a for statement need not be integers, although extra care must be used when using real numbers. We might, for instance, wish to tabulate the values of the function

y = 2x2 — 8x + 1 for values of x from —2 to +2 in steps of 0.1.

We could write: for x := —2 step 0.1 until 2 do print x,sameline, 2 x xt2—3xx+1,; The difficulty here is that real numbers are not held with complete accuracy so that when we expect x to attain the value 2 it may in fact become 2.000001 say. This would overshoot our limit of 2. 0 and the final value would be omitted.

In a simple case like this we could make x integer and write: for x := —20 step 1 until 20 do

print x/10, sameline, 0.02

x xt2—0.3

x x +1;

Alternatively we could increase the upper limit by, say, one tenth of the step value giving: for x := —2 step 0.1 until 2.01 do print x, sameline, x x (2 x x — 3) + 1;

This would ensure that the value of the function corresponding to x = 2 was computed. In the last example, what is known as nested multiplication has been used. Only two multiplications are needed here, and this method can save valuable computing time when more complex functions have to be evaluated many times. The variable following the word for is is defined during the execution of each pleted its value is undefined. Its value is terminated by jumping out of the cycle

Section 3.2

called the controlled variable. Its value loop but after all the cycles are comwill be retained, however, if the loop by means of agoto statement in the loop.

Output Setting Procedures

To provide control of layout all Algol compilers have special procedures to allow the programmer to output his results in a variety of forms. One of these procedures, sameline, has already been mentioned. It will be recalled that the effect of this procedure was to cause all the elements in the print list following the word sameline to be printed on the sameline. Clearly a new line could be obtained by means of the double string quotes facility “L’’;

The effect of print a, sameline, b, “L”’, c, d; when a = 1,b = 2,c = 3,d = 4 is to print

1 3

2 4

A further note on output. It was mentioned in section 3.1 that the variable x might take a value slightly larger than 2, and the print as shown would print many superfluous decimal places. This can by requesting a certain number of significant figures in the print For example:

for x := —2 step 0. 1 until 2.01 do print freepoint (2), x; would give

—2.0 —1.9 etc.

26

controlled statement be avoided out.

—1.0 —. 90 —. 80 etc.

Here the decimal point is moved when the magnitude of the number is less than 1, and although the result is better the layout does not look good. We can improve on this by asking for one digit before the decimal point and one after it. Thus:

print aligned (1, 1), x; would give —2.0 —1.9 etc. —1.0 —0.9 —0.8 etc.

The full range of input/output control and setting procedures will be described fully in manufacturers’ manuals.

Section

3.3

The for-list

The for list: The main restriction on the construction for variable := variable step increment until final do statement;. (Where initial,increment, final are either variables or numbers

of type

real or integer) is that unless the problem lends itself to some fairly disciplined increments, either constants such as 1, 2, 3, 4, etc, or expressions, for example, (x + 2)/N, this construction is not really applicable.

There is a simpler yet very powerful form of the for statement. the form for variable := n,,N9,n3,n,

This is of

etc do S1;

where S1 is a statement. This will cause the loop statement

S1 to be obeyed with the variable taking

the values of n, ng n, until the list is exhausted. is often

It should be noted that S1

a compound statement.

Consider the following piece of program. It will read in a day number (that is a number representing the position of a day in the year, 1 for January 1, 31 for January 31, 32 for February 1,365 for December 31, etc), and print out the date corresponding to the day number. date calculation;

1

begin integer dayno, month, year, j;

2

comment These names are fairly self-explanatory. j is to be used as the controlled variable in the for statement for counting the number of days in the various months;

more:

switch sss := fin, more;

3

year := 1967;

4

read dayno; month := 1;

5 6

27

comment Year really could be any value but let us consider this typical year

(not a leap year). The above statement sets month to 1 at the beginning of each calculation. Notice the values in the list below corresponds to the number of days in the months of the year January—31 February—28 March—31, etc;

a 8

for j := 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 do begin dayno := dayno — j; month := month + 1;

9

if dayno < 0 then begin dayno := dayno + j;

10 11 12

month := month —1; goto fin end

end; This is the compound statement which is the loop statement of the for loop;

print “L”, digits (2), sameline, dayno, ‘/’, month, digits (4), /’, year; comment This statement prints out the results of the calculation.

13

14 LS

fin:

Digits (2)

means that, for this particular print out, until this setting is countermanded, only a minus sign or a space and two digits of each number will be output. The statement below causes the computer to try and read in more numbers—clearly this program is never going to finish—it will simply try and read in more numbers until it is either stopped manually or it runs out of tape in the reader;

16

goto more

1G

end;

Data 1, 31, 365, 29, 47, 183, 97, 296 The output would look like this

1/ $1/. 31/ 29/ 16/ 2/ W) 3/

07 1967 1/2 1967 12/1967 1/ 1967 2/ 1967 7/ 1967 -4/> 1967 10/ 1967

N.B. the lines of the program above have been numbered for easy reference for the next exercise. Lines of comment have not been counted in the numbering scheme.

Now work through the program tabulating the line number and the values of the variables after execution of the instruction on that line. Use data 31 and 97.

Line No | j_ | Dayno | Month

28

Comments

A correct verson

of the list is shown below.

The real work of the program above is carried out in lines 8-13 and these steps are examined in detail here.

This table follows through the working of lines 5-16 for the sets of data given.

Value of variable after the execution of the statement j Dayno Month

Line No

for sc= 31728, 31, etc 31 31 dayno := dayno — j; 31 onwdtouwif dayno < 0 then

31 31 31 0 31

1 1 2 2

31

1

read dayno; month := 1;

Comments

this is true (dayno = 0) therefore the statement (comp) following this will be obeyed.

10 1d

month := month — 1; goto fin

31

14 &15

print etc

31

16

obey print statement next.

1

goto more

month := 1; for j := 31,28, etc dayno := dayno — j; if dayno < 0 then

for j := 31,28, etc. dayno := dayno — j;

if dayno < 0 then for };= ete dayno := dayno — }; if dayno < 0 then for j := then

dayno := dayno — j; 0 then

if dayno < -i aoe wonmnnowno

month := month — 1; goto fin print, etc

31/1/1967 is printed. Obey read statement next on line 5.

97

read dayno

om

31

97 is next number

on tape (say) 31 31 31

97 97 66 66

1 1 2 2

28 28 28 31 31 31 31 30 30

66 38 38 38 7 7 7 —23 fi

2 3 3 3 4 4 4 5 5

30

a

This is not true therefore compound statement skipped, end of loop statement reached and control is transferred to line 7.

ditto

ditto

True this time, therefore obey the loop statement and print statement next.

4

7/4/1967, etc.

29

Section

3.4

Flow Charts

Flow Charts: Where the organisation of a program is complex, it is often helpful to set out the flow of instructions in diagramtic form before writing the program itself. Some of the symbols used are shown in Fig. 10. A flow chart for the date calculation program is shown in Fig 11. It will be noticed that jump instructions are clearly indicated by arrowed flow lines by-passing the normal sequence of instructions. These lines aré particularly helpful in visualising the flow through the program. The smaller loop represents the for statement. In charting a statement of this kind we should normally examine the variable to see if the end of the statement has been reached. In this instance, however, we are assuming that a number greater than 365 would not be read in. If in fact we offered 400, the for list would become ex-

hausted and the program would go on to print 35/13/1967. program never terminates there is no STOP START symbol.

Note that since this

symbol corresponding to the

It will not be our practice to show flow charts for programs except in special cases, but the reader may find it helpful to construct his own for some of the examples used.

START

FINISH

a2 Sa

Indicates the start or finish of a program.

reset month to 1

Indicates an input or output operation.

J:=no. of days in current month

deduct j from dayno and increase

month by 1

Indicates a straightforward assignment statement.

evs add backJ to dayno and deduct 1 fromm

FALSE Indicates a conditional statement. The exits are labelled to show which condition

they correspond to.

print dayno, Jo year

—_p>—___ Indicates general flow between boxes.

Fig.11.

Fig.10. 30

Flow chart symbols

Flow chart for the date calculation program of section 3.3

Section

3.5

The Nested

for-statement

So far we have considered only single for statements. It is possible to “nest” these statements. Consider the following example where i and j are integers. for i:= 1 step 1 until 10 do begin S1; for j := 2 step — 1 until —5 do 10

8 82; S3 end;

The inner loop containing the statement S2 is controlled by the for-statement: for j := 2 step —1 until —5 do and therefore ment because end) and this is obeyed 8 x

is obeyed eight times, but it is included in the outer loop stateit is part of the compound statement (enclosed between begin and outer loop is obeyed 10 times. Therefore the inner loop statement 10 or 80 times in all. To summarise then

Statement S1 is obeyed 10 times

Statement S2 is obeyed 80 times Statement S3 is obeyed 10 times To clarify these points (with i, j,k, 1and m defined as integers) try the follow-

ing more complicated example. S, S, obeyed ?

How many times is each statement S, S, S,

for i := 1 step 1 until 7 do——— for j := 5 step 2 unti! 7 do

begin

= Sj; 2?

for k := 0 step 3 until 12do 4 S23

— =

for 1 := 2 step 1 until 9 do for m := 1, 3, 6, 9 do 84; [Se

eee

end;

One of the best ways to work this out is to set out the program —

7| 2

as follows:

for i:= 1 step 1 until 7 do for j := 5 step 2 until 7 do begin S,; So; for k := 0 step 3 until 12 do

|

S.; for 1 := 2 step 1 until 9 do

8} 4

for m :=1, 3,6,9 do Sa; 5 end;

The brackets enclose each separate loop and the figures give the number of times it is obeyed.

31

S,,S,S, are obeyed 7 x 2 = 14 times S, is obeyed

7x 2X

5= 70 times

S, is obeyed 7 x 2 x 8 X 4 = 448 times Now try and correct the following program. reckoner multiplication table shown below.

It is designed to print out a ready

example ready reckoner begin integer numacross, numdown print ‘ ‘L’ ready reckoner table’;

print “L2”;

for numdown = 1 step 1 until 10 do print sameline, digits (2), numacross

X numdown

end;

The output of the program

should look like this:

Ready reckoner table

2

— So

3

_ ol

4

5

6

7

8

9

booO

bo ol

(ow)oO

(ow)ol

weoO

AS ol

50

60

70

80

90

10

oloO

OmMDADUARWNHH

10

20

30

40

The following program

is the correct version of the above problem.

example ready reckoner; begin integer numacross, numdown; print ‘ ‘L’ ready reckoner table’; for numacross := 1 step 1 until 10 do begin print “L”; for numdown := 1 step 1 until 10 do print sameline, digits(3), numacross end end; Here

are the reasons

Line No 1

2 4

5 6 7

100

x numdown

re WD ODmDADU

for the corrections:

Semicolon is ALWAYS required after the title in Elliott 903 Algol. Semicolon is ALWAYS required after declaration. This for statement is required to ensure the program constructs the whole table

The begin is required to go with the end in line 8. A colon is required as a value is assigned to numdown. Three digits are required to print 100.

Section 3.6

Arrays

Arrays. It is often necessary in using a computer to deal with a large number of different variables. If, for instance, we were dealing with 100 employees' salaries, it would be very inconvenient to have to declare one hundred differ-

32

ent identifiers. We could call them El, E2,...E100, but we should still have to put all these names in our declaration. ALGOL

provides us with a much simpler, and very powerful method of dealing

with batches of variables. The variables are declared as an array, and the elements are referred to by a single identifier followed by a number in square

brackets. Thus the employees mentioned above would be referred to as E[1], E[2],...E[100]. The declaration, however, is very short, and if the employees' salaries were in integer form we would have:

integer array E[1: 100]; Note that in the declaration the minimum and maximum reference numbers are separated by a colon. These numbers are known as the lower and upper array bounds respectively. The individual reference numbers are known as subscripts, and array elements are often referred to as subscripted variables. The great advantage of arrays is that the subscripts may be referred to by means of other variables. Thus E[n] would refer to the salary of an employee whose number is determined by the currently computed value of n. Arrays of real numbers may be treated in exactly the same way. In this case, however, the word real may be omitted from the declaration. The compiler takes array to mean real array. The word integer must always be used for integer arrays.

As an example let us consider a program to read in 100 real numbers, print each number, and also their average value. Example

average;

begin integer n; real sum; array a [1 : 100]; Sum: —10; for n := 1 step 1 until 100 do

begin read a [n]; print a [n]; sum := sum + a [n]; end;

print sum/100,

‘= average’

end: Note that if this were part of a larger program the 100 numbers would be left in the array for further processing. The subscripts may be referred to not only by means of a single variable but also by algebraic expressions, e.g., a [xt 2 + y]. In this case the expression would be evaluated, and if it were of type real it would be adjusted to the nearest integer, since only integer subscripts are allowed.

In general all integers negative, positive, or zero are allowed as subscripts in ALGOL. A restriction of Elliott 903 AILGOL is that subscripted variables are not allowed as controlled variables in for statements, i.e., we cannot have:

for a [r] := etc. In the program

above we could not say

print sum/n.... since the value of n is not defined as we left the for loop by exhausting it. Had we left it by a goto instruction the value of n would have been preserved.

The reader might care to adapt the example so that it will operate on any number of items of data up to 100, the first number on the data tape being the number of items of data.

33

The adjustment to ‘average’ might be carried out as follows:1 example average 2;

2

begin integer i, n; real sum; array a [1 : 100]; for i := 1 step 1 until n do

3 4 5

begin read a[i]; print a [i]; sum := sum + a [i

6 vi

end;

8

read n;if n > 100 then print ‘ ‘L’ too many data’ else begin sum := 0;

9 10 11

print sum/n, ‘= average’ end end;

If the test in Line 3 is not applied the computer would indicate an error as soon as i had increased beyond the upper array bound of 100. Care must always be taken to ensure that the subscript remains within the bounds.

An array containing only gross salaries would not be very much use, and we might want our array to contain much more information about the employees. These various factors would be needed for each employee, and it would be convenient to consider these under code numbers as well. This can be done by using a two dimensional array. Supposing for instance we have a small firm. employing six labourers paid hourly. We might construct a table as follows:-

Employee No Hourly pay rate Overtime

rate -

Hours

worked

Hours

overtime

Total pay

-

-

If we call the array a we can refer to a particular compartment

by specifying

the row and column, e.g.,a [3,4]. This would contain the number of hours worked by employee No 4. Note that the two subscript numbers by a comma.

are separated

Let us consider a program to read in the first four rows of the above array and work out the gross salaries and store them in the fifth row. example

pay calculation;

begin integer i, j; array a [1: 5,1:

6];

for j := 1 step 1 until 6 do for i := 1 step 1 until 4 do

read a |i, j]; comment

The data is scanned

for j := 1 step 1 until 6 do

wo oP De

a column

at a time;

a [5,j)c=a(1,j)x0a.[3,3) asl2,d) eal

6

7

comment The gross pay is calculated for each employee in turn; This is, of course, not a complete program, but it illustrates the compactness

which can be obtained by using arrays. 34

Section

3.7

Standard

Functions

The programming language ALGOL was designed for scientific use, but it is quite suitable for writing commercial procedures. There are some complex operations which have to be performed several times; these range from calculating the logarithm of a number to searching a file for a certain entry. ALGOL has been designed to allow the user to define commonly used procedures and insert these into his program (how this is done will be dealt with later). Certain mathematical procedures have been built into the language. These procedures are called standard functions.

DESCRIPTION

OF STANDARD

FUNCTIONS

It should be noted that the names of these standard functions are usually reserved for this purpose and may not be used in the program with any other meaning. E is an expression of type either real or integer. Ris a real expression.

Name

Description

abs (E)

The modulus (absolute value) of the value of E e.g. abs (—3) = 3 abs! "(3) =.3

sign (E)

The sign of the value e.g. sign (10. 4) sign (3.284) sign (0)

of E = —1 = +1 == we()

The last case is unlikely to occur is real.

sqrt (E) sin (E)

in practice,

where the expression

The square root of the value of E e.g. sqrt (6.25) = 2.5 N.B. E 20. The sine of the value of E. E is usually in radians. 180° = 7 radians

cosine of the value of E. E is usually in radians. principal value of tan~! E. natural logarithm of the value of E.(E > 0.) exponential function of the value of E, i.e.e#.

cos (E) arctan (E) In (E) exp (E)

The The The The

entier (R)

This function transfers the value of R from type real to type integer. The value assigned is the value which is the largest integer not greater than the value of R.

These functions may be used freely on the right hand side of an assignment statement. Examples

Algol Statement

Mathematical

y :=1n (cos (ET? 2));

log cos E?

z := sqrt (sqrt (x)); k= ain. (x)1. 2: j= Sin i(xt2); m := In (arctan (k));

4x sin?x sin x? log tan ~1k

Equivalent

35

Section

3.8

The for-while construction

There are many occasions in programming where a loop of instructions has to be obeyed a number of times, but we do not know at the outset what this number is. In the example in Section 3. 6, for instance, we calculated the gross pay for six employees assuming that the hours that they worked were punched as data. It would be more convenient to present the computer with the hours worked by each man on each job he did during the week’and make the computer calculate his total hours. This means that we should have to allow for a variable number of entries for each man. In ALGOL there is a variation of the for statement which allows us to do this quite simply. This statement is of the form: for

n:= 1,n + 1 while

do ;

The relationship is some criterion which is applied to a variable in the statement, and as long as this relationship is true the loop will continue to be executed. In the above example n might be used only as a counter increasing by one each time the loop is executed. There is complete freedom of choice of the first value and the step value for n (which need not be an integer). Thus we could have:-

for x := 21, x—-y while y tT 2 —z > 0.001 do etc and here y could vary from one loop to the next. There are Firstly, it able must calculated,

two important points to note about this type of for statement is not possible to use step. until in conjunction with while. The varihave assigned to it an expression from which successive values are although the first few values may be listed.

e.g.for n := 1, 2, 3,n + 2 while etc.

Secondly, when using while the loop is always executed at least once, unless the controlled variable is given its initial value before the for-statement is started. eo. x = 05 for x := x + 1 while etc

Let us consider how this might be applied to our wages calculation. We shall assume that the employees are given numbers 1 to 6 and that the data is punched as pairs of numbers, the first being the employee’s number and the second the number of hours worked for a particular job. We shall also assume that a separate tape will be available, punched with employee’s number and his basic hourly rate in pennies, and that anything over 40 hours is payable at time and a quarter.

This time we shall use three arrays called hrs, pay and rate; to signal the end of the input data we shall punch two zeros. Example, further pay calculations;

begin integer i, j, 1, s,d; real job; array hrs, rate, pay[0:6]; comment

we have to declare a zero array bound, otherwise

an error

would be indicated when reading the hours tape; for i:= 1 step 1 until 6 do

begin read j, rate[j]; hrs[j] := 0 end; comment the above instruction hours array; for i:= 1,i+ 1 while j > 0do begin read j, job;

a, 36

hrs[j| := hrs[j| + job

reads in the basic rates and clears the

comment we now have the total hours worked by each man, and we go on to increase any hours over 40 by a quarter, so as to allow for overtime, and then calculate gross pay; for i := 1 step 1 until 6 do

begin if hrs [i] > 40 then hrs[i] := hrs[i] + (hrs[i] — 40) x 0. 25; pay [i]: = hrs[i] x rate[i] end; comment we shall now go on to adjust pay to the higher penny, convert to pounds, shillings and pence, and print the gross pay. First, we shall print headings for the output; print ‘ ‘L’ employee no ‘S10’ gross pay’, ‘ L822’ £ sd; for i := 1 step 1 until 6 do

begin d := entier (pay[i] + 1.0); 1 :=d + 240; d := d— 240 x 1; See C-li2: d:=d—sx 12;

print i, “S13”, sameline, digits(2), 1, s, d end end;

Note that where several arrays have the same bounds, they may be listed in the declaration and the bounds written only once, as above. This program is a rather clumsy way of using the for. .. while construction. Its power is more apparent in mathematical work, and we shall discuss one such application below. The main use of while is in mathematical and scientific computing, and we shall now give an example of this. We hope that our non-mathematical readers will bear with us on this expedition into the mathematical jungle.

Consider the calculation of log.x for —1< We should normally start with the series

lo

Re

(1+x)=x—=

tee

2

x
0

ee

=

~as

tt

Der vee oo Vb ¥

eee

=

oe

a2n—1\1ty

2n=1

3

is

(oe)

Seay.

5

Hae

+,,..ete.

Below we show a program to read the value of y and print log y. This logarithm is available as a standard function in ALGOL but we shall use the series to evaluate it to four places of decimals.

37

Example, logarithm; begin integer i; real x, logx, y, 2, last; switch ss := another, finish; x comment the program will read x and calculate log(x) until a negative so is read. y is a constant factor for the power series, and z is arranged that odd powers are obtained; another: read x; if x < 0 then goto finish;

logx: = 0; y:= ((x — 1)/(k + 1))T 232: = (z + 1)/(x— 1); for i:=1,i + 2 while abs (last — logx) > 0.00005 do

begin last := logx; 1 = TB XK AR

logx: = logx + 2 x 2/i end;

comment increasing i by 2 at each loop saves last must be assigned the last value of logx at ready for the comparison with the next value. for the trivial case of x = 1, since to find z we Zero;

arithmetic within the loop, the start of each loop This program will fail should have to divide by

print ‘ ‘L’ log’, sameline, x, ‘to base e = ’, aligned (1, 4), logx, In(x); comment the function goto another; finish:

In(x) has also been printed for comparison;

end;

For x = 0.5, 0. 75, 1. 5, 2. 0, 3. 0, 4.0, -1 on the data tape the output is as follows:-

log log log log log log

. 5000000 . 7500000 1.500000 2. 000000 3.000000 4.000000

to to to to to to

base base base base base base

e = — 0. 6931 — 0. 6931 e = — 0. 2877 — 0.2877 e 0.4055 0.4055 e 0.6931 0.6931 e 1.0986 1.0986 ST Let 1.3863 e a 1.3863

Note that by using identifiers y and z a lot of computing is saved compared with saying:

logx := logx + (((1 — x)/(1 + x)) Ti)/i; Apart from assigning the latest value of logx to last our loop involves only two multiplications, one division, one addition and two assignments, and the form of the while statement ensures that the desired accuracy is reached. One must be careful of course not to ask for more accuracy than the computer is capable of. The analysis of errors in computing can be complicated, and we shall not discuss it in this book. Many other mathematical problems can be dealt with using while. For example when finding roots of polynomials, the roots can be searched for by calculating

the function y = f(x) for a series of values of x and looking for a change in the sign of y. The statement would be something like:

for x: = 1,x + 0.1 while sign (lasty) = sign(y) do etc. x1 would be the last value of x to be used for calculating an earlier root or starting the search and looping would stop as soon as the sign of y changed. The current value of x could be stored and the search for further roots continued up to some maximum value of x. From the approximations so found the roots might then be calculated to any desired accuracy. Better approximations can be found by decreasing the increment of x in the neighbourhood of the root, and nesting the values on either side of the root.

38

For discussion of these and other techniques the reader is referred to books on numerical analysis.

% AXIS

Fig.12.

The curve shows a hypothetical function y = f(x). For successive values of x, y is calculated and the while statement would cause looping to stop on reaching points Yo, yg, y7(These are the points following a change of sign of f(x))

Even in the simple form given so far the for statement is a powerful tool. However the structure of the list in a for statement can be composed of many combinations of the forms previously defined. The following examples illustrate the scope of this facility. for a: = 1 step 2 until k,k + 3 step 9 until 182 do S1; for j: = —7,j + 3,+1 step 7 until 15 do S1; for i: = 1, 2,3 step 2 until 7,10, z + 1 while z < 7,10 do S1;

39

Section 4

More Advanced Techniques

In this section some of the topics which are not essential for a preliminary study of ALGOL are covered. The treatment is basic but it is left to the reader to implement some of these concepts in a sophisticated manner.

Section

4.1

Blocks

The reader will already be familiar with the use of begin and end to convert a group of simple statements into a compound statement. This, it may be remembered, was used in conjunction with if, and conditional statements, and for clauses. Also the reader will be familiar with the fact that all programs are enclosed between the words begin and end. Thus the whole program is a compound statement with one or more declarations immediately after the begin. This type of compound statement is called a block. All the programs so far considered have consisted of only one block, but it is possible to have programs made up of a number of blocks. The following structure is quite valid. begin

declarations for Block 1

statements for Block 1 (if any) begin declarations for Block 2 statements for Block 2 begin declarations for Block 3 statements for Block 3 end; more statements for Block 2 (if any) end; more statements for Block 1 (if any) begin declarations for Block 4 statements for Block 4 end;

more statements for Block 1 (if any) end; Notice here how blocks have been nested one inside the other in the same as we nested for loops earlier.

way

The restriction placed on the use of variables declared in block headings, is that they are only able to be used in that block or in any inner block (i.e. any block declared within that block). Thus, returning to our first example, all the variables declared at the head of block 1 are available in blocks 2, 3 and 4. However, those declared in block 2 are only available in blocks 2 and 3, but not in blocks 1 and 4. Variables declared in block 3 are valid only in that block, and similarly with block 4.

40

Consider now the following example: block test example; begin real a; comment this is block 1;

Sa5

begin integer b; comment this is block 2; $2;

begin integer tax; comment this is block 3; $3;

end; $4;

begin real cost price; comment

this is block 4;

S5; end;

S6;

begin ———————————_

com ment this is block 5,

7.

end; 88;

end; S9; end;

Note: S1-S9 are statements and declaration.

represents a statement, not a

41

In complicated structures of this kind the insetting of the program between corresponding begins and ends is most important. Without this it would be more difficult to see how the blocks were nested. Now answer the following questions on this piece of program.

(1)

What is wrong with the above piece of program?

(2)

In which blocks are the following variables valid? a b tax

costprice

(3)

Which of the following statements are invalid and why?

Statement

Form

A=, bie. 0; b: =aT

2-5;

tax: = costprice X 22. 5/100; Acie:

costprice: = (a + b) X 3; by =a. > b; costprice: = costprice + 3; (EbS Seb ap Aue

aul Dias The following answers examined carefully.

to the questions above on block structure

Q1: Block 5 is not a block but tain any declarations.

should be

a compound statement because it does not con-

Q2: In the diagram below a V indicates the blocks in which the variables can be used.

o x Q @ “4 m4 as)

>

42

Q3: The table below sets out the validity of variables occuring in statements S1 to S9. The V or x in the bottom row indicates the validity or otherwise of the statement.

Statement

Variable

* Note that this statement is also invalid as it is a multiple assignment statement with mixed types.

There is another restriction on the use of blocks in the program. That is, that a block may only be entered at its beginning and not at any other point. thus: title; begin real a, b; begin integer c, d; switch sss := jump;

jump:

end; goto jump

end; is invalid because goto “jump”

causes

the program

to try and enter the inner

block in the middle. If “jump” labelled the second begin in the program it would be quite valid, provided the switch declarations were made earlier. Sorting: To illustrate the use of blocks we will consider a fairly common example, namely that of sorting the numbers in an array into ascending order by the method of ordering pairs. The method works as follows: (1) each pair of adjacent elements in the array (called a) is examined i.e.

a{1] and a[2], a[2] and a[3], a[3] and a[4]—a[n — 1] and a[n] and a test is

made to see if the first one is the smaller. If this is so then the next pair is considered, if this is not so then the numbers are interchanged before the next pair is considered. (2) Step 1 is repeated n — 1 times where n is the number element in the list to

be sorted.

43

It is suggested that the reader should set up a group of playing cards in random order and apply this sorting procedure to these to convince himself of the validity of this method. Anyone who does this will notice that in most cases he will not need to repeat Step 1 n —1 times as the cards will be in order before this time. However, it will be simpler to avoid trying to overcome this problem at this stage. This procedure could be described in a flow chart as illustrated below.

interchange

act] & a[ct+]]

count N+1) No Fig. 13. The flow chart for sorting by ordering pairs

44

The ALGOL program to perform these operations is as follows: To read sort and print numbers; begin integer n; read n; comment n is the number of numbers to be read in; begin integer ct, countn; real temp;

array a[1:n]; comment notice here that this time we have used a variable de- _ clared in the outer block as a bound of the array declared in the inner block. This is quite valid;

for countn:

= 1 step 1 until n do read a [countn];

comment this for statement reads in the elements for countn: = 1 step 1 until n — 1 do for ct: = 1 step 1 until n— 1 do

of the array;

if act] >alct +.1] then begin temp: = a{ct + 1];

a{ct + 1]: = al[ct]; a{ct]: = temp end; comment notice here that we have used for-loops to simplify the logic of the flow chart with regard to the counting operations. The interchanging of the elements is done in the compound statement following then; for ct: = 1 step 1 until n do print a[ct] end of block; end;

Own Variables Any type or array declaration in an inner block may be preceded by the word own. The effect of this is to preserve the values of these variables so that if the inner block is re-entered,a process could continue from the point it reached before the exit. In view of the difficulties experienced in implementing this

facility (this is particularly true of arrays) many algol compilers do not allow own arrays

and some

Section 4.2 A string between but they ing, and

exclude the use of the own facility altogether.

Input and Output of Strings

is defined in ALGOL as any sequence of ALGOL characters appearing string quotes ‘and’. We have already used these extensively ir. output, have other uses, and many compilers provide for the reading in, storthe later printing of strings.

In Elliott 903 ALGOL, strings to be read in must be punched on a data tape, the string quotes being punched at the start and finish of each string on the tape. When the characters are read in, they are stored in such a way that each element of an integer array accommodates three characters. An integer array of sufficient size to hold all the characters, including the string quotes, must be declared. In addition, an integer variable must be declared. This will be used as a marker for beginning each string in turn. Special procedures called instring and outstring are used to deal with these strings. Supposing that the array is de-

45

clared a[1:100],and the integer is called m. When the first string is read m must be set equal to 1, so that we might have: Wises

for n: = 1, 2,3 do instring (a, m); This would cause three strings to be read in from the tape and m would then give the position of the next vacant element of a. The value of m for the beginning of the second and third strings could be obtained by counting the characters input and allowing for the packing of three to each element of a. The value of m would not be important if the strings were to be output in the same sequence as they were stored. We could then say: m:s= 1;

for n: = 1, 2,3 do outstring (a, m); and the three strings would be printed out exactly as read in. Note that if no new line characters appeared in the strings they would all be printed on the same line. It may be necessary to print strings in some other order than that in which they are stored, the easiest way of dealing with them is to declare a second integer array to hold the successive values of m giving the starting points of the strings. Suppose our second integer array is called str and that we have three strings to read in, we could have:

vg 1 for n: = 1, 2,3 do

begin str[n]: = m: instring (a, m) end;

We could now output the strings in a different order, e.g.:-

for n: = 3,1,2 do

begin m: = str[n]; outstring (a, m) end; This would cause the third, first and second strings to be output in that order.

As an illustration of the use of this facility we could return to our pay calculation of section 3.6. Instead of giving employees numbers we could read in their names, and print them in the output. Let us assume that the first data tape contains the following names ee's number order:

‘Smith ‘OBrien

?, °;

‘Jones “Mckay

’, ~~:?

‘Brown

*,

in employ-

‘Robinson ’,

all the strings have been extended to 10 characters between quotes, by adding spaces, So as to take up the same space on output. The program would be as follows: Example, pay calculations with names;

begin integer i, j,1,m,s,d; real job; array hrs, rate, pay/0:6];

integer array name [1:24];

comment 24 locations in the array are needed to store three characters per element and each string beginning in a new element;

for i := 1 step 1 until 6 do instring (name, m); for i:= 1 step 1 until 6 do

begin read j, rate[j]; hrs[{j] := 0 end; 46

for i:=1,i+ 1 while j > 0do begin read j, job;

hrs[j] := hrs[j] + job end;

for

i:= 1 step

1 until 6 do

begin if hrs[i] > 40 then hrs[i] := hrs[i] + (hrs[i] — 40) x 0. 25; payli] := hrs[i] x rate[i] end; print ‘ ‘L’ employee

S167 a0

Se

‘S10’ gross pay’,

sd?

;

ile

for i:=1

step 1 until 6 do

begin d := entier(payli] + 1.0); . .

d + 240; d— 240 x 1;

° .

d + 12;

Hou a d—12x~°s; . il an Qe .

print outstring (name, m), “S6”, sameline, digits(2), 1, s,d end end;

For the following data and the names above we shall show the output for this program. 1, 78, 2, 78, 3, 96, 4, 96, 5, 100, 6, 114, DO 270, 0,4, 1,.21,.2, 20, 4,17,3,19,4,.20,2,19,3, 21,1, 20,4, 2,5, 29, 6.31,

5, 15, 6, 14 EXAMPLE EMPLOYEE

SMITH JONES BROWN ROBINSON OBRIEN MCKAY

Section 4.3

GROSS PAY ee oD 15 16 11 15° 0 8 ea ee | ise tort 18 15 1 recat BS Beat

Boolean Operations

Another powerful facility in ALGOL is its ability to perform logical operations. In purely mathematical terms these are called Boolean operations. They are useful, however, to mathematician and non-mathematician alike. Boolean variables are declared by the declaration boolean at the head of a program along with the other declarations. Boolean algebra is concerned with the truth or falsehood of relationships between entities.

Boolean relationships were encountered when we considered if statements. The course of action to be followed there depends on the truth or falsehood of the relationship. In fact this relationship can be any boolean expression. Therefore:

if a > b and c < 10000 then S1;

is valid ALGOL; the word and has been introduced here to join the two relationships a > b and c< 10000. The statement S1 will only be obeyed if they are both true.

47

A boolean variable can be assigned a value true or false either directly by saying: a: — true;

b: = false;

or less directly by saying for example:

a: — c >d; (a is true if c > d otherwise it is assigned the value false)

b: = 1 = m; (b is true only if 1 = m otherwise it is false) c: = if sqrt (tot) > 12 then false else true; true and false are basic words in ALGOL. Boolean operators: The word and was an example of a boolean operator. The other boolean operators and their mathematical symbols (which are used in ALGOL 60 to represent these operations) are tabulated below. We will continue to write them as words in ALGOL programs and reserve the mathematical symbols which will be unfamiliar to many readers for descriptive sections of the text. Word

Mathematical

Comment

Symbol not and

a IX

or

V

equiv impl

= D>

logical equivalence logical implication

To define exactly the effect of these operations in boolean expressions their effects have been tabulated below.

Value of expression ii

values of the variable

seni true

true

true false false

false true false =o

Read the above table carefully then do these examples:

given that

a is true

Lo

b is false c is true

90) = 3}, © i =e

2.08

p=2

What is the value of the boolean variable x in the following cases? 3G = By AIL oy Koes elle—a 9° x metre:

x =n#pD((c

=a)vVm> pp).

x =(1(n#p))An+p=4;

Answers (here the expressions above have been written with words rather than symbols).

48

Expression

Value

xX: = a and not b;

true

x == bor, h ==ips x: = 1 >m equiv true;

true false

x: = (not (n ~ p)) and p + p = 4;

true

x: =n =p

impl ((c equiv a) or m > p);

true

Any disheartened readers should realise that these boolean expressions are more complex than those found in most programs, but are merely here to illustrate the text and test the reader's understanding of this section.

Section 4.4

Non-type

Procedures

We have seen how a loop of program may be executed many times before the sequence of written instructions is continued. This can easily be programmed using a for statement when all the operations are to be carried out at one stage of the calculation. There are many occasions, however, when a particular series of operations has to be carried out several times at different places in a program. By a careful arrangement of jumps it would be possible to switch to this sub-program and then back to the correct point in the main program, but the returns to sequence would have to be counted and the programming would be tiresome.

If, for instance, we had a program that calculated a pay roll and worked in £s and decimals of a £ we would wish at certain stages of the program to convert the figures to £ s d and print these out. This situation could be represented diagramatically as shown below. title; begin switch sss: = a, b, c, d, conv;

conv:

— Ie See X: = gross pay;

Ih /i

goto conv;

’ -

2

eva

Ol

=

the exit; so ——————

>, goto sss\i];

/ | Mey bent ws ie

CO Ol

eu

:



4

X: = tax;

/ 17

i

hee



ze

4

60 (ea oe

i ‘





Xiv== nett pay;

4

ri

goto conv; —-—-—-— af

it

this piece of program

will convert x to £ s d and print it out and then return to the main program immediately after

Le

X! = superannuation; goto conv; — —-—— —/ =

comment

ij at i;

A: !

;

goto conv; ------------ :

4 a

id

/ J

end; 49

In this diagram we have used the goto with a subscripted switch name. The reader may recall that when switches were mentioned earlier they were referred to as switch arrays. In fact the separate labels can be referred to indirectly by writing a suffix in square brackets after the switch list identifyer. Consider the following situation:-

switch sss: = first, next, more, less, again;

goto sss [i]; If i = 4 then this would be equivalent to writing goto less because less is the 4th name in the switch list. Similarly if i = 2 then this would be equivalent to writing goto next; etc. In ALGOL we are able to choose a name for a sub-program of this kind, and then, in the main program, when we use the name, the sub-program is executed and a return is automatically made to the main sequence of instructions immediately after the call for the sub-program. These sub-programs are called procedures, and we have already met several of them without mentioning how they fit into this system.

One of the simplest procedures is sameline, which suppresses the output of new line characters between items in a print list. We shall show how the programmer can declare and use his own procedures for carrying out operations which have to be repeated many times. In executing the procedure sameline, no information is required about any variable in the program, and we therefore do not need any quantity in brackets after the name. In using the procedure freepoint, however, we have to indicate

the number of significant figures required (¢.g., freepoint (5),) and this number is called a parameter; sameline is consequently known as a parameterless procedure. Many procedures require only one parameter, although this may be an arithmetic expression which has to be evaluated by the computer. The number of parameters depends, in fact, on how many quantities are required from the main program in order that the procedure can be carried out.

In choosing a name for our procedure we use the usual rules for forming identifiers, making sure that it does not clash with any of the standard function names or with any other name in our program. Before we can refer to one of our procedures in a program it. There are in fact three parts required as follows: -

we must declare

(i) procedure heading (ii) specification (iii) procedure body. Part (i) consists of the basic word procedure followed by its name, and in parentheses

a list of parameters.

Part (ii) informs the computer what kind of entities the parameters are.

50

Basic words are used for this

integer real boolean array label string

are used to indicate the type of the parameter. indicates the parameter is an array. indicates the parameter is a label indicates the parameter is a string

A specification part is not compulsory in ALGOL 60, but many compilers require it, and it is safer to include it. It does not matter in what order the specifications are made.

Part (iii), this starts with begin followed by any declaration of variables tem-

porarily required for the procedure calculation, the actual instructions, and end. This part is in fact a program block.

The three parts together constitute the procedure declaration. All this may seem rather indigestible, but an example will help to make it clear. Let us consider a procedure, called sort, to sort an array of numbers into descending order by ordering pairs. The declaration would be as follows:

procedure sort (a, n); array a; integer n; comment we are letting the procedure know that an array-has to be dealt with, and how big it is. The latter information is conveyed by n. We do not have array bounds in a specification as we would in a declaration; begin real x; integer r,s; for r := 1 step 1 until n — 1 do for s := 1 step 1 until n— 1 do

if als] < als + 1] then begin x : = a[s]; als] := als + 1]; als'+ 1] :=x end

end sort;

Using the procedure name as comment after the final end is useful in separating the procedure from the rest of the program. Procedures are best declared immediately after variables for the main program. (This is compulsory with

many compilers.) In using this procedure we should simply say:

sort(b, k); This stands as a statement on its own, and b and k will be an array and an integer in the main program respectively. It does not matter what identifiers we use for parameters in the procedure declaration, they will be replaced by quantities from the main program when the procedure is executed. The parameters in the declaration are called formal parameters. They are just symbols which enable us to write an intelligible piece of program. When we call the procedure to be executed in our program we then show what are called actual parameters on which the operation will be carried out. An actual parameter may be a number instead of an identifier. We could thus have: sort (c, 200);

The formal parameters do not have to be different in name from the actual ones, and in some cases it may be convenient to use the same ones. If there is any duplication of identifiers between the procedure working variables (r,s and x in the above example) and main program variables, the latter will be suppressed as would happen in a program block. The values of the main program variables would not be affected.

51

There is a difference between the way that this sort procedure is used and the method of using mathematical functions, and the reader may care to ponder this before we discuss it further.

Section 4.5

Type Procedures

We mentioned in the last section that the procedure sort must stand on its own as a program statement, and this is where it differs from a procedure which produces a numerical result. The latter can also, and usually does, stand on the right hand side of an assignment statement, either alone or as part of an expression. Where a procedure produces a numerical result, this result will be either of type integer or type real (it is possible to have boolean procedures, but we shall not consider them here) and this type must be included in the procedure declaration. Thus if we had to write our own procedure to calculate sines, we should declare it:

real procedure sine(x); real x; etc. Procedures which produce numerical results of a specified type are known as type procedures or function designators, and others as non-type procedures. The distinctions between the two are tabulated below:

TYPE

PROCEDURES

produce a numerical

NON-TYPE result.

PROCEDURES

in general, produce a non-numerical result.

Declaration begins real procedure

or integer procedure.

Declaration begins procedure.

May stand as a statement on its own, but is usually used on right hand side} of assignment statement or as part of an expression or relation.

May only be used as a statement on its own. (The device setting procedures in Elliott ALGOL print lists are a special case in this respect.)

As an example of a type procedure the standard deviation of an array.

we shall consider

one which calculates the

real procedure sigma (a, r); array a; integer r; begin integer i; real mean, s; mean := s := 0; for i:= 1 step 1 until r do mean

:= mean

+ ali] ’

mean := mean/r; for i:= 1 step 1 until r do s :=s + (mean — a[i])? 2; sigma := sqrt(s/r) end sigma;

Note that in a type procedure the final numerical value is assigned to the procedure identifier. This identifier can only be used on the left hand side of an assignment statement in the procedure body, except in the case of recursive procedures. Examples

of calls of this procedure could be:

‘= sigma(c, n) + sigma (b, n); or print sigma (k, r), ‘= standard deviation’;

52

Actual parameters in a procedure call need not be single identifiers, but may be expressions. Thus we could say:

m := sigma (k, (a + c)/2); In this case, each time the procedure body mentions the formal parameter

r,

the expression (a + c)/2 is evaluated and this result substituted for r. This method of dealing with parameters is known as calling by name. If an actual

parameter is an expression and occurs in a for loop, the expression may have to be evaluated a large number of times to produce the same result each time. This is wasteful of computing time. It would be more economical to work out the expression once and substitute this answer for the formal parameter.

This facility is obtained automatically in ALGOL if the appropriate parameter is called by value rather than by name. If we wish to call the parameter r by value in the procedure sigma, our procedure heading will be: real procedure

sigma (a, r); value r; array a; integer r;

Note that valu’: is a basic word, and that it must occur before the specification. If several parameters are to be valued they are listed after value and separated by commas. e.g., value x, y, a; If an array is called by value the whole array is copied into the procedure working space. Care must be taken to ensure that there is adequate space for this. Readers might care to write a type procedure to find the largest element of an array of integers.

One solution to the problem posed above is as follows:

integer procedure max(a, r); integer array a; integer r; begin integer k, m; ing) 5S

al1];

comment a[1] has been selected quite arbitrarily here, any other element of the array would do. If it is not the largest it will be replaced. Having selected a[1], we may, of course, begin our scan at the second member of a; for k := 2 step 1 until r do

if alk] > m then m := alk]; max max then max :=

ali!

if ali] < min then min := ali]

end; range := max — min end range;

This is a case where a type procedure might be used as a procedure

statement

i.e., range (age, x, max, min); The value of the range would not be assigned, but max and min would be obtained. This is an appropriate case for the formal and actual parameters max and min to have the same name. In fact, since the procedure body is treated as a program block, provided that max and min are valid identifiers in the block where the procedure call occurs, we need not mention them as parameters, but simply assign values to them in the procedure body. This is rather a dangerous practice, however, since it can easily be overlooked when the values of variables, which are not parameters, are altered by a procedure. Let us look now at a complete program including a procedure declaration and its callinthe mainprogram. The program will readina batch of numbers, sort them into descending order and print them in this order, and also print their arithmetic mean. The first number on the data tape will be an integer equal to the number of data to be dealt with. Example, numbers; begin integer n, i; procedure sort (a, r); array a; integer r; begin integer i, }; comment the i declared here would be different from that declared in the main program; real x; for i:= 1 step 1 until r — 1 do for j := 1 step 1 until r — ido

if alj] < alj + 1] then begin x := alj];

aljl:=alj+ 1); alj a 1] Fist OK

end the bottom of the stack at each scan. earlier than the previous one; end sort;

This places the smallest number at We therefore end each scan one step

real procedure mean (a, r); integer r; array a; begin real sum; integer i; sum

:= 0;

for i := 1 step 1 until r do sum: = sum + ali]; mean := sum/r end mean; comment this completes our first declaration. We shall go on to read the number of elements of data, and then start a new block, declaring our array now that its size is known; read n;

begin array num[1:n]; for i: = 1 step 1 until n do read nunji]; sort(num, n); print ‘ ‘L’ data’;

for i: = 1 step 1 until n do print freepoint (3), num[i|; 54

print ‘ ‘L2? ’, freepoint (3), mean(num,.n) ‘= mean?’

?

end of block;

end of program; Specimen data and output are shown below: data 5, 1.7, 3. 4, —2, 7.5, —5; data 7.50

3. 40 1.70 —2. 00 —5. 00 1.12 = mean

Recursive procedures

It is possible in Algol to write a procedure where the procedure body contains a call of the procedure itself. Procedures of this type are called recursive procedures. An example may help clarify the idea.

integer procedure factorial (n); integer n; factorial: = if n = 1 then 1 else n x factorial (n — 1); Note that where no declaration of a local variable is required and as procedure body consists of only one statement, no begin and end are required. A number dures.

of compilers including the Elliott 903 do not allow recursive proce-

Our final chapter will be devoted to the discussion of a single large program. This does not involve any real mathematics, but is used to process data on the use of the computer at the Royal Liberty School, Romford.

55

Section

A

5

Case

Study

In every computer installation it is necessary to keep a log book showing how the computer is being used. This information is required by the management for statistical and accounting purposes to analyse how this very expensive piece of equipment is operating. The Computer Department at the Royal Liberty School has to perform this task and like many other installations, the computer is used to perform the detailed analysis. We have selected our log book analysis program for a case study to round off this series of notes on ALGOL programming. There are two main reasons for this choice. Firstly, it is a non-mathematical application of ALGOL. Secondly, it is a working program which has now been used to analyse our computer log for 16 months, and has demonstrated that it works in practice. This is not to say that we are entirely satisfied with the program. It is now being rewritten to provide us with extra facilities. We collect our information in the simplest possible way, by coding it as a series of numbers. The information we require for each period of use of the computer is as follows: (i) Date (ii) Who is using the computer

(iii) What the computer is being used for (iv) How long it is used

The Date

The most compact way of writing the date is as one number, this number senting the position of the day in the year. 1 for January 1 2 for January 2 31 for January 31 32 for February 1 365 for December 31

repre-

The User

Each user of the computer is given a number. In a commercial concern, each department would be given a number. In our case we have given each school and each other user of the machine a user number. For example:

0

is assigned to the Royal Liberty School Computer

1

is assigned to the Royal Liberty School for the use of boys;

2

is assigned to Hornchurch College of Further Education;

3

is assigned to Harold Hill Grammar

Up to 40 numbers

56

Department;

School.

are allowed for under the present system.

The Use

Computers are put to a variety of uses. These will vary according to the nature of the use of the computer installation. Some of the headings we use are given below:

1,

Systems planning and development;

2,

Staff teaching programs, testing and development;

3,

Students’ program

testing and development.

Up to 20 different uses have been allowed for in this system.

Time We could require the user, when making his entry, to put down the number of minutes used, but this is asking him to do something the computer can do for him. In fact, we have a digital clock, and the user notes down the starting and finishing time in the 24 hour system. e.g., twenty past eleven a.m., 11. 20; a quarter past nine p.m., 21.15. The digital clock is vital here, because the actual numbers means the time is noted accurately and quickly.

being displayed

The time is recorded as four numbers, two for each of the starting and finishing times. We are therefore required to note a total of seven numbers for each entry into the log. These

are, in order:

day number, user number, use number, time (4 numbers). For ease of punching, we have a standard log sheet which is completed daily. See Fig. 14. The column headed Comments is used for noting any special information concerning an entry. This information gives us a complete specification of how the computer has been used. What information do we wish to derive from this input data?

(1)

We wish to know on which dates the machine was used, and for how long on

(2)

An analysis by Use is required for statistical purposes; for instance we

each day.

need to know that we are not tending to spend too much time on computer maintenance. (3)

Similarly, we need an Analysis by User whereby we can check how each user's workload on the machine is developing.

(4)

For further detailed analysis of how each user utilised the machine a display map showing the breakdown of each user's time under the heading of each use.

(5)

A notice for each user is produced on the computer, and this tells the user

how much time he has used each month. the computer

this notice also serves

In the case of Paying Users of

as a check on the bills which they

eventually receive. (6)

(7)

A memorandum to the Borough Treasurer setting out how much money is to be collected from each Paying User of the computer and asking him to collect this money.

Sundry figures showing the serviceability of the computer, the total time used, and the total money earned, etc.

57

Finally, totals for each use and each user are accumulated for each year and are printed out for re-input next month. Figure 14 shows some typical entries in the computer log. It does not cover a whole month, the full range of uses, or users, but it is illustrative

(8)

data for this case study. There are two main sources of difficulty with this system of keeping the log. Firstly, we rely on human beings to note the time, etc, and being human they occasionally either make an erroneous entry or fail to make one at all. We do endeavour to detect erroneous entries as much as possible. For instance, if the time the machine stops being used is earlier than that at which it started, our audit procedure fails. This is most often caused by a time being entered on the log sheet as for example, 1.15 rather than 13.15. Invalid user, use or day numbers are also detected.

Secondly, work is sometimes entered against the wrong use number. This is of course impossible to check when the computer is being used on a large scale. is set out in the systems

The way in which our logbook analysis system works

flow chart (Fig. 15). The main input document (the log sheet) is punched at

ROYAL

DAY

Real

USER

LIBERTY

SCHOOL TIME

NO.

canis USE

0 al PS,

a

at

oasi

ul

- MACHINE

DEPARTMENT

TIME

hrs.

OFF

a Oui 1S ast

2e).02

fee ie HO ae19 | |05 3 |12 | 42 13

[13 19 [13 | 54

LOG

INITIALS

min,

45|8 [57| 03/9 |11 27| 9 |55 |

o | 5 L vel mee 3 |= eA ee BS

ON

e| min.

93

(nein

COMPUTER

Routine bests

ete ee eee

eee

sea See

3

|

1

Ie

5

| Ais

eee: et | 3 | 13) 25 | 14 |52 1 3__|15| 01 |15 |58 5 S__| 16 |02 | 16 | 54|

| 999 Fig.14

lagi

ia

ub

t

A typical log sheet for the Royal Liberty School computer

the end of each month. The form in which it is typed is the same as the handwritten original shown in Fig. 14. The whole data for a complete month amounts to more than 20 pages.

Log book sheet

Converted to paper tape

Read data into computer

Analyse

Punch

data

analysis

Print up analyses by Day, User & Use Print Notices to

User

Punch notices computer

As

nD

Read the rates of charge tape into the computer store

Previous

summary

|.

Calculate charges

Up-date

running totals

Prepare bills

;

i

Next month

Fig.15

Print bills eruces 0 Treasurer

om.

em al Summary tapes for re-input next month

Flow chart for the log book program

Having described the main features of the log book analysis program, the detailed program which is used to perform this analysis on the computer is shown here. Several comments have been inserted to indicate where the various sections of the program begin.

Log book analysis program begin

2 Royal Liberty School Computer Department;

comment the program has been divided into five sections. Section A covers all the procedures called by the main program. These have

59

been written as procedures because they are required for other purposes. For this program alone many of them would have simply been written and tested as blocks; integer userno, useno, hn, mn, hf, mf, dayno, daynol, day, month, year, dur, k, 1,1, n, x, tot, tdt;

integer array store [0:40, 1:20], dly [1:31], anbr [0:40], anbs [1:20], rate [20:40];

real tinc; boolean audit; switch sss := analysis, moredata, rrate, error, skipit; procedure

date (dayno, day, month); value dayno; integer dayno, day, month; begin switch sss := fin; integer j, year; year := 1967; comment procedure date outputs the date given the day number year. Eg, 1st January = 1, 31st January = 31, 1st February = 32, etc; day := dayno; month := 1; for j := 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 do begin day := day — j; month := month + 1; if day < 0 then begin day := day + j; month := month — 1; goto fin end

in the

end;

fin : print ‘ ‘L’ ’, digits (2), sameline, day, ‘/’, month, digits (4), ‘/’, year; end date; procedure calcdur (hrson, minon, hrsoff, minoff, dur); value hrson, minon, hrsoff, minoff; integer hrson, hrsoff, minon, minoff, dur; begin

comment procedure calcdur calculates the time elapsed between two defined times expressed in hours and minutes on the 24 hour clock. It is assumed that no entry will start on one day and finish on another; if hrson = hrsoff then dur := minoff — minon else begin if hrson 40 or useno >20 or hn >hf or mn >60 or mf >60 or hn > 24 or hf >24 or dayno >366 or daynol >366 and daynol ~ 999 then begin audit := false; print userno, useno, hn, mn, hf, mf, daynol; print ‘audit failure’ end;

calcdur (hn, mn, hf, mf, dur); accumulate

(store, dly, userno, useno, dur, day);

if daynol ~ dayno and daynol ~ 999 then date (daynol, day, month); comment this section of the program causes us to escape from the reading loop when a day number of 999 is read in. This is the trigger to indicate the end of the data tape and is punched as the last number on that tape; if daynol = 999 then goto analysis; dayno := daynol; goto moredata; comment section C. This section outputs the tables of analysis by use, user and date. This is followed by a complete display of the array store. This facility is provided so that further analysis of the use of the computer can be made quite simply if it is required. This section of the program performs summation from the store and fills arrays for the analysis of use and the analysis of user; analysis: if not audit then stop; comment stop is a control procedure which causes the program to terminate; for userno := 0 step 1 until 40 do for useno := 1 step 1 until 20 do

begin i"

nbs [useno] := anbs [useno] + store [userno, useno]; anbr [userno] := anbr [userno] + store [userno, useno]

end; print ‘ ‘L6’ analysis by user’; for k := 0 step 1 until 40 do begin print k;

conmin (anbr [k]) 62

end;

print ‘ ‘12? analysis by use’;

for k := 1 step 1 until 20 do | begin print k; conmin (anbs [k]) end; comment this section of the program analyses the use of the computer by date and tabulates it and the number of hours used; print ‘ ‘L6’ analysis by date ‘L’’; for day =: 1 step 1 until 31 do

begin print digits (2), day, ‘/’, sameline, month, ‘/’, digits (4), year, Se". conmin (dly [day]) end; print ‘ ‘L6’ display of analysis’; for k := 0 step 5 until 30 do

begin print ‘ ‘L’ userno’, digits (2),k, ‘to’,k + 4; for n := 1 step 1 until 20 do for m | < logical operator> | < sequential operator>

= arithmetic operator > :: = 1 he is < sequential Ree aS =aa to| if| nen else| for |do*

: | :=| 11 | step| until) while| comment : (1) jia ::ny begin| end - (en

ae

= own

ee a

| Boolean | integer

string |label|value

| real | array

| switch

| procedure

Delimiters have a fixed meaning which for the most part is obvious, or else will be given at the appropriate place in the sequel.

Typographical features such as blank space or change to a new line have no significance in the reference language. They may, however, be used freely for facilitating reading. For the purpose of including text among the symbols of a program ing “coinment” conventions hold: The sequence

of basic symbols:

the follow-

is equivalent to

; comment ; ; begin comment < any sequence not containing; >; begin end < any sequence not containing end or; or else > end.

By equivalence is here meant that any of the three structures shown in the lefthand column may, in any occurrence outside of strings, be replaced by the symbol shown on the same line in the right-hand column without any effect on the action of the program. It is further understood that the comment structure encountered first in the text when reading from left to right has precedence in being replaced over later structures contained in the sequence.

2.4

2.4.1

Identifiers

Syntax

< identifier > :: = |

|

* do is used in for statements. It has no relation whatsoever preliminary report, which is not included in ALGOL 60.

to the do of the

95

2.4.2

Examples q Soup Vi7a a34kTMNs MARILYN

2.4.3

Semantics

Identifiers have no inherent meaning, but serve for the identification of simple variables, arrays, labels, switches, and procedures. They may be chosen freely (cf., however, section 3.2.4 Standard functions). The same identifier cannot be used to denote two different quantities except when these quantities have disjoint scopes as defined by the declarations of the program (cf. section 2. 7 Quantities, kinds and scopes, and section 5 Declarations).

2.5

Numbers

2.5.1

Syntax

:: = | < digit> :: = | + | — :: = :: = 10~ integer > :: = < unsigned integer> | |

:: = | |

:: =

| + | — 2.5.2

Examples

0 077

—200.084 —,083,,—02 +07. 43,8 aT .5384 9,34, ,+10 a4 #071300 -( 23-4 +3 +5

2.5.3

Semantics

Decimal numbers have their conventional meaning. scale factor expressed as an integral power of 10. 2.5.4

Types

Integers are of type integer.

5. 1 Type declarations). 2.6

The exponent part is a

All other numbers

are of type real (cf. section

Strings

2.6.1 Syntax :: =

|

:: = | ‘) |

:: =‘) 96

|

|

2.6.2

Examples

5k, ,—*[[[¢

‘,..This 2.6.3

.,

peneea aWSa eB)

is .,a.4string

»)

Semantics

In order to enable the language to handle arbitrary sequences of basic symbols the string quotes ‘and? are introduced. The symbol _, denotes a space. It has no significance outside strings. Strings are used as actual parameters of procedures tion designators and 4. 7 Procedure statements).

2.7

(c.f. sections

3.2 Func-

Quantities, kinds and scopes

The following kinds of quantities are distinguished: labels, switches, and procedures.

simple variables, arrays,

The scope of a quantity is the set of statements and expressions in which the declaration of the identifier associated with that quantity is valid. For labels see section 4.1.3.

2.8

Values and types

A value is an ordered set of numbers (special case: a single number), an ordered set of logical values (special case: a single logical value), or a label. Certain of the syntactic units are said to possess values. These values will in general change during the execution of the program. The values of expressions and their constituents are defined in section 3. The value of an array identifier is the ordered set of values of the corresponding array of subscripted variables (cf. section 3.1.4.1).

The various types (integer, real, Boolean) basically denote properties of values. The types associated with syntactic units refer to the values of these units.

3.

EXPRESSIONS

In the language the primary constituents of the programs describing algorithmic are arithmetic, Boolean, and designational, expressions. Constituents of these expressions, except for certain delimeters, are logical values, numbers, variables, function designators, and elementary arithmetic, relational, logical, and sequential, operators. Since the syntactic definition of both variables and function designators contains expressions, the definition of expressions, and their constituents, is necessarily recursive. processes

:: = | < Boolean expression> |

Variables

Syntax

:: = < identifier > < simple variable > :: = < subscript expression> :: = < subscript list> :: = < subscript expression> | ,< subscript expression > roe < array identifier > :: = list>] subscript < [ er > identifi :: = variable pted

le

:: = | < switch identifier > |

| |

:: = |

:: =,| ) : ( 2: =

|

:: = | () :: = < actual parameter part> 3.2.2

Examples

sin (a— b)

j(v + s,n) R

S(s — 5) Temperature: (T) Pressure: Compile (:=)) Stack: (Q) 3.2.3

(P)

Semantics

Function designators define single numerical or logical values, which result through the application of given sets of rules defined by a procedure declaration (cf. section 5.4 Procedure declarations) to fixed sets of actual parameters. The rules governing specification of actual parame ters are given in section 4. 7

98

Procedure statements. function designator. 3.2.4

Not every procedure declaration defines the value of a

Standard functions

Certain identifiers should be reserved for the standard functions of analysis, which will be expressed as procedures. It is recommended that this reserved list should contain:

abs(E) sign(E)

for the modulus (absolute value) of the value of the expression E for the sign of the value of E(+ 1 for E > 0,0 for E = 0,—1 for

sqrt(E) sin(E) cos(E) arctan(E)

for for for for

In(E)

for the natural logarithm of the value of E

exp(E)

for the exponential function of the value of E (e£).

E :: = x |/| + :: = | | | () :: = .| 1 :: = |< term> < multiplying operator > < simple arithmetic expression> :: = | | :: = if < Boolean expression > then :: = |

else

Examples

Primaries:

wii + 2,8] cos(y + z xX 3) (2 —3/y + vu! 8) 99

Factors:

omega sum 7 cos(y + z X 3) 7.394,,) —87 wli+ 2,8]? (a — 3/y + vuT 8) Terms:

U

omega X sumT

cos (y + z X 3)/7. 394,

Simple arithmetic

— 87 wli + 2, 8] T (a— 3/y + wuT 8)

expression:

U — Yu + omega x sumT

cos (y + z X 3)/7. 394,, — 87 wli + 2, 8]

t (a— 3/y + wut 8) Arithmetic expressions: w X u— Q(S + Cu) T 2 if q >0 thens + 3 x Q/Aelse2xS+3xq if a17 then U/V else if k ~y then V/U else 0 a X sin (omega x t) 0.57,, 12 x a[N x (N — 1)/2, 0] (A X arctan(y) + Z)T (7 + Q) if q then n — 1 elsen if a is equivalent to the construction

else if true then 3.3.4

Operators and types

Apart from the Boolean expressions of if clauses, the constituents of simple arithmetic expressions must be of types real or integer (cf. section 5.1 Type declarations). The meaning of the basic operators and the types of the expressions to which they lead are given by the following rules: 3.3.4.1

100

The operators

+,—,and

x have the conventional

meaning

(addition,

subtraction, and multiplication). The type of the expression will be integer if both of the operands are of integer type, otherwise real.

3.3.4.2 The operations /< factor > and ~+ both denote division, to be understood as a multiplication of the term by the reciprocal of the factor with due regard to the rules of precedence (cf. section 3.3.5). Thus, for example a/b x I/(p — q) X v/s means

((((a x (b~1)) x 7) x ((p — q)~1)) x v) x (s72) The operator / is defined for all four combinations of types real and integer and will yield results of real type in any case. The operator ~+is defined only for two operands both of type integer and will yield a result of type integer, mathematically defined as follows:

a +b = sign (a/b) x entier (abs(a/b)) (cf. sections 3.2.4 and 3.2.5).

3.3.4.3 The operation 1 denotes exponentiation, where the factor is the base and the primary is the exponent. Thus, for example 27 n7? k means (2n)kK while

27 (nT m) means 2(0™) Writing i for a number of integer type, r for a number of real type, and a for a number of either integer or real type, the result is given by the following rules:

aTi

Ifi>0,axaxX...X a (itimes), of the same type as a. If i= 0, if a +0, 1, of the same

type as a, if a = 0, undefined.

tae 0,30 2 001 / (aX a . a) (the denominator has —i factors), of type real, if a = 0, undefined.

alr

If a > 0, exp (r X In(a)), of type real. ia 0,17 7 0,0. 0, of type. real, if r < 0, undefined. If a | | (< Boolean expression >) < Boolean secondary> :: = | 4 < Boolean factor > :: = | ~ < Boolean secondary>

< Boolean term> :: = < Boolean factor > | < Boolean term> ~< Boolean factor> :: = | > < Boolean term> :: = | = < implication> < Boolean expression> :: = | else < Boolean expression>

3.4.2

Examples

x=—2

Y2-V 72:—5Az—d>qtT 2 Ded Srey, Cae A ates deem tt ifk welseh (implies), =

(equivalent), is given by the following function table. bl b2

false false

false true

true false

true true

Hilo!

true

true

false

false

bl =b2

true

false

false

true

3.4.6

Precedence

of operators

The sequence of operations within one expression is generally from left to right, with the following additional rules:

3.4.6.1 According to the syntax given in section 3.4.1 the following rules of precedence hold: first: arithmetic expressions according to section 3. 3.5. 22 Doge second: < < third: fourth: fifth: sixth: Oh At TT eas seventh:

3.4.6.2 The use of parentheses will be interpreted in the sense given in section 3.3.5. 2.

3.5

Designational expressions

3.5.1

Syntax

:: = | < switch identifier > :: = < identifier > < switch designator > :: = [|] :: = | < switch designator> | () :: = | else < designational expression > 3.5.2

Examples

iy pg

Choose [n — 1] Town [if y < unconditional statement>

|

:: = | :: = end | < statement>;< compound tail > :: = begin | ;

:: = begin < compound tail > :: = ; :: = |. : < compound statement >

:: = | : :: = | This syntax may be illustrated as follows: Denoting arbitrary statement s, declarations, and labels, by letters S, D, and L, respectively, the basic syntactic units take the forms: Compound statement: L:L: ...begin’S ;S;.-.S;'S end

104

Block: L:L:...beginD;D;...D;

8;S;...S;

S end

It should be kept in mind that each of the statements S may again be a complete compound statement or block. 4.1.2

Examples

Basic

statements:

a= phd go to Naples START: CONTINUE: Compound

W := 7.993

statement:

begin x := 0 ; for y :=1 step 1 until n do x := x + Aly]; if x > q then go to STOP else if x > w — 2 then go to S; Aw: St: W := x + bob end Block: Q: begin integer i, k; real w; for i := 1 step 1 until m do for k := i+ 1 step 1 until m do

begin w := ali, k]; Ali, k] := Alk, i]; A[k, i] := w end for i and k end block Q 4.1.3

Semantics

Every block automatically introduces a new level of nomenclature. This is realized as follows: Any identifier occurring within the block may through a suitable declaration (cf. section 5 Declarations) be specified to be local to the

bloek in question. This means (a) that the entity represented by this identifier inside the block has no existence outside it, and (b) that any entity represented by this identifier outside the block is completely inaccessible inside the block.

Identifiers (except those representing labels) occurring within a block and not being declared to this block will be non-local to it, i.e. will represent the same entity inside the block and in the level immediately outside it. A label separated by a colon from a statement, i.e., labelling that statement, behaves as though declared in the head of the smallest embracing block, i.e. the smallest block whose brackets begin and end enclose that statement. In this context a procedure body must be considered as if it were enclosed by begin and end and treated as a block.

Since a statement of a block may again itself be a block the concepts local and non-local to a block must be understood recursively. Thus an identifier, which is non-local to a block A, may or may not be non-local to the block B in which A is one statement.

4.2 4.2.1

Assignment statements Syntax

:: = : =|

: =

:: = | :: = | < Boolean expression >

105

ws

i)

Examples

i)

S.—

pO

ne

nee ieee

n oe cr il A:= B/C —v—q x8 S Iv,k + 21 == 3. — arctan(S x zeta) Yay, Vv 1) 4.2.3

Semantics

Assignment statements serve for assigning the value of an expression to one or several variables or procedure identifiers. Assignment to a procedure identifier may only occur within the body of a procedure defining the value of a function designator (cf. section 5. 4.4). The process will in the general case be understood to take place in three steps as follows:

4.2.3.1 Any subscript expressions occurring in the left part variables are evaluated in sequence from left to right. 4.2.3.2

The expression of the statement is evaluated.

4.2.3.3 The value of the expression is assigned to all the left part variables, with any subscript expression having values as evaluated in step 4. 2. 3.1. 4.2.4

Types

The type associated with all variables and procedure identifiers of a left part list must be the same. If this type is Boolean, the expression must likewise be Boolean. If the type is real or integer, the expression must be arithmetic. If the type of the arithmetic expression differs from that associated with the variables and procedure identifiers, appropriate transfer functions are understood to be automatically invoked. For transfer from real to integer type the transfer function is understood to yield a result equivalent to

entier (E + 0.5) where E is the value of the expression. The type associated with a procedure identifier is given by the declarator which appears as the first symbol of the

corresponding procedure declaration (cf. section 5. 4. 4).

4.3

Go to statements

4.3.1

Syntax

< go to statement> :: = go to < designational expression> 4.3.2

Examples

go to 8

go to exit [n + 1] go to Town [if y Othenn:=n+1 ifv > u then V:q :=n+melse gotoR if s s — 1 then goto S 4.5.3

Semantics

Conditional statements cause certain statements to be executed or skipped depending on the running values of specified Boolean expressions, 4.5.3.1 If statement. The unconditional statement of an if statement will be executed if the Boolean expression of the if clause is true. Otherwise it will be skipped and the operation will be continued with the next statement. 4.5. 3.2 Conditional statement. According to the syntax two different forms of conditional statements are possible. These may be illustrated as follows:

if B1 then S1 else if B2 then S2 else S3; S4 and if B1 then S1 else if B2 then S2 else if B3 then S3; S4

107

Here B1 to B3 are Boolean expressions, while S1 to S3 are unconditional statements. S4 is the statement following the complete conditional statement. The execution of a conditional statement may be described as follows: The Boolean expressions of the if clauses are evaluated one after the other in sequence from left to right until one yielding the value true is found. Then the unconditional statement following this Boolean is executed. Unless this statement defines its successor explicitly the next statement to be executed will be S4, the statement following the complete conditional statement.. Thus the effect of the delimiter else may be described by saying that it defines the successor of the statement it follows to be the statement following the complete conditional statement. The construction

else < unconditional statement > is equivalent to else if true then

If none of the Boolean expressions of the if clauses is true, the effect of the whole conditional statement will be equivalent to that of a dummy statement. For further explanation the following picture may be useful: i ft L if B1 then S1 else if B2 then S2 else S3; $4

Lyears

thn

Bl false 4.5.4

Go to into a conditional

ob den aes a! B2 false

statement

The effect of a go to statement leading into a conditional statement follows directly from the above explanation of the effect of else.

4.6

For statements

4.6.1

Syntax

:: = | < arithmet ic expression > step < arithmetic expression> until < arithmetic expression> |

< :: = | , clause> :: = for := do shia a :: = < statement> | :

0 then go to Element exhausted;

Statement S; VeVi, go to L1;

where V is the controlled variable of the for clause and Element exhausted points to the evaluation according to the next element in the for list, or if the step-until-element is the last of the list, to the next statement in the program. 4.6.4.3 While-element. The execution governed by a for list element of the form E while F, where E is an arithmetic and F a Boolean expression, is most concisely described in terms of additional ALGOL statements as follows: Iga} & WA ges oe if — F then go to Element exhausted; Statement S; go to L3; where the notation is the same

4,6.5

as in 4.6.4.2

above.

The value of the controlled variable upon exit

Upon exit out of the statement S (supposed to be compound) through a go to statement the value of the controlled variable will be the same as it was immediately preceding the execution of the go to statement. If the exit is due to exhaustion of the for list, on the other hand, the value of the controlled variable is undefined after the exit.

109

4.6.6

Go to leading into a for statement

The effect of a go to statement, outside a for statement, which refers to a label within the for statement, is undefined.

4.7

Procedure

4.7.1

statements

Syntax

.

:: = | < switch identifier > | :: =

| |

|

:: =,| ) : ( < actual parameter list> :: = | < actual parameter list> < actual parameter > :: = | ( ) :: = < procedure identifier> < actual parameter part> 4,7.2

Examples

Spur (A) Order: (7) Result to: (V) Transpose (W,v + 1) Absmax (A, N, M, Yy, I, K)

Innerproduct (A[t, P, uJ, B[P], 10, P, Y) These examples correspond to examples given in section 5. 4. 2. 4.7.3

Semantics

A procedure statement serves to invoke (call for) the execution of a procedure body (cf. section 5.4 Procedure declarations). Where the procedure body isa

statement written in ALGOL the effect of this execution will be equivalent to the effect of performing the following operations on the program at the time of execution of the procedure statement:

4.7.3.1 Value assignment (call by value). All formal parameters quoted in the value part of the procedure declaration heading are assigned the values (cf. section 2.8 Values and types) of the corresponding actual parameters, these assignments being considered as being performed explicitly before entering the procedure body. The effect is as though an additional block embracing the procedure body were created in which these assignments were made to variables local to this fictitious block with types as given in the corresponding specifi-

cations (cf. section 5.4.5).

As a consequence, variables called by value are to

be considered as non-local to the body of the procedure, but local to the fic-

titious block (cf. section 5. 4. 3).

4.7.3.2 Name replacement (call by name). Any formal parameter not quoted in the value list is replaced, throughout the procedure body, by the corresponding actual parameter, after enclosing this latter in parentheses wherever syntactically possible. Possible conflicts between identifier s inserted through this process and other identifiers already present within the procedure body

will be avoided by suitable systematic fiers involved.

changes of the formal or local identi-

4.7.3.3 Body replacement and execution. Finally the procedu re body, modified as above, is inserted in place of the procedure stateme nt, and executed. If the procedure is called from a place outside the scope of any non-local quantity of the procedure body, the conflicts between the identifi ers inserted through this process of body replacement and the identifiers whose declarations are valid at the place of the procedure statement or function designator will be avoided through suitable systematic changes of the latter identifiers.

110

4.7.4

Actual-formal correspondence

The correspondence between the actual parameters of the procedure statement and the formal parameters of the procedure heading is established as follows: The actual parameter list of the procedure statement must have the same number of entries as the formal parameter list of the procedure declaration heading. The correspondence is obtained by taking the entries of these two lists in the same order. 4.7.5

Restrictions

For a procedure statement to be defined it is evidently necessary that the operations on the procedure body defined in sections 4. 7.3.1 and 4.7.3.2 lead to a correct ALGOL statement. This poses the restriction on any procedure statement that the kind and type of each actual parameter be compatible with the kind and type of the corresponding formal parameter. Some important particular cases of this general rule are the following:

4.7.5.1 If a string is supplied as an actual parameter in a procedure statement or function designator, whose defining procedure body is an ALGOL 60 statement (as opposed to non-ALGOL code, cf. section 4. 7. 8), then this string can only be used within the procedure body as an actual parameter in further procedure calls. Ultimately it can only be used by a procedure body expressed in non-ALGOL code.

4.7.5.2 A formal parameter which occurs as a left part variable in an assignment statement within the procedure body and which is not called by value can only correspond to an actual parameter which is a variable (special case of ex-

pression). 4.7.5.3 A formal parameter which is used within the procedure body as an array identifier can only correspond to an actual parameter which is an array identifier of an array of the same dimensions. In addition, if the formal parameter is called by value, the local array created during the call will have the same subscript bounds as the actual array. 4.7.5.4 A formal parameter which is called by value cannot in general correspond to a switch identifier or a procedure identifier or a string, because these latter do not possess values (the exception is the procedure identifier of a procedure declaration which has an empty formal parameter part (cf. section 5. 4, 1) and which defines the value of a function designator (cf. section 5.4.4). This procedure identifier is in itself a complete expression).

4.7.5.5 Any formal parameter may have restrictions on the type of the corresponding actual parameter associated with it (these restrictions may, or may not, be given through specifications in the procedure heading). cedure statement such restrictions must evidently be observed. 4.7.6

Deleted.

4.7.7

Parameter

In the pro-

delimiters

All parameter delimeters are understood to be equivalent. No correspondence between the parameter delimiters used in a procedure statement and those used in the procedure headingis expected beyond their number being the same. Thus the information conveyed by using the elaborate ones is entirely optional.

4.7.8

Procedure body expressed in code

The restrictions imposed on a procedure statement calling a procedure having its body expressed in non-ALGOL code evidently can only be derived from the

1 it

characteristics of the code used and the intent of the user, and thus fall outside the scope of the reference language.

5. DECLARATIONS Declarations serve to define certain properties of the quantiities used in the program, and to associate them with identifiers. A declaration of an identifier is valid for one block. Outside this block the particular identifier may be used

for other purposes (cf. section 4. 1. 3).

:

Dynamically this implies the following: at the time of an entry into a block (through the begin, since the labels inside are local and therefore inaccessible from outside) all identifiers declared for the block assume the significance implied by the nature of the declarations given. If these identifiers had already been defined by other declarations outside they are for the time being given a new significance. Identifiers which are not declared for the block, on the other hand, retain their old meaning.

At the time of an exit from a block (through end, or by a go to statement) all identifiers which are declared for the block lose their local significance. A declaration may be marked with the additional declarator own. This has the following effect: upon a re-entry into the block, the values of own quantities will be unchanged from their values at the last exit, while the values of declared variables which are not marked as own are undefined. Apart from labels and formal parameters of procedure declarations and with the possible exception of those for standard functions (cf. sections 3.2.4 and 3.2.5) all identifiers of a program must be declared. No identifier may be declared more than once in any one block head. Syntax

:: = | |

< switch declaration > |

5.1

Type declarations

0.1.1

Syntax

Stype list> :: = | , :: = real | integer | Boolean < local or own type> :: = | own

< type declaration> :: = 5.1.2

Examples

integer p,q,s own Boolean Acryl,n 5.1.3

Semantics

Type declarations serve to declare certain identifiers to represent simple variables of a given type. Real declared variables may only assume positive or negative values including zero. Integer declared variables may only assume positive and negative integral values, including zero. Boolean declared variables may only assume the values true and false. In arithmetic expressions any position which can be occupie d by a real declared variable may be occupied by an integer declared variable. For the semantics of own, see the fourth paragraph of section 5 above.

112

5.2

Array declarations

9.2.1

Syntax

:: = < arithmetic expression> :: = < arithmetic expression > :: = : :: = | , :: = [ ] | ,< array segment> :: = | , < array declaration> :: = array | array

5.2.2

Examples

array a,b; c¢|7;: h;2 2mi,s [—2 *10] own integer array Alif c < 0 then 2 else 1: 20] real array q|—7 : —1] 5.2.3

Semantics

An array declaration declares one or several identifiers to represent multidimensional arrays of subscripted variables and gives the dimensions of the arrays, the bound of the subscripts and the types of the variables. 5.2.3.1 Subscript bounds. The subscript bounds for any array are given in the first subscript bracket following the identifier of this array in the form of a bound pair list. Each item of this list gives the lower and upper bound of a subscript in the form of two arithmetic expressions separated by the delimiter : The bound pair list gives the bounds of all subscripts taken in order from left to right. 5.2.3.2 Dimensions. the bound pair lists.

The dimensions

are given as the number of entries in

5.2.3.3 Types. All arrays declared in one declaration are of the same quoted type. If no type declarator is given the type real is understood. 5.2.4 5.2.4.1

Lower upper bound expressions The expressions will be evaluated in the same way as subscript ex-

pressions (cf. section 3.1. 4. 2). 5.2.4.2 The expressions can only depend on variables and procedures which are non-local to the block for which the array declaration is valid. Consequently in the outermost block of a program only array declarations with constant bounds may be declared.

5.2.4.3 An array is defined only when the values of all upper subscript bounds are not smaller than those of the corresponding lower bounds. 5.2.4.4 block. 5.2.5

The expressions will be evaluated once at each entrance into the

The identity of subscripted variables

The identity of a subscripted variable is not related to the subscript bounds given in the array declaration. However, even if an array is declared own the values of the corresponding subscripted variables will, at any time, be defined only for those of these variables which have subscripts within the most recently calculated subscript bounds.

113

5.3

Switch declarations

J, ded

noyntax

< switch list> :: = | < switch list>, < switch declaration> :: = switch < switch identifier > := < switch list> 5.3.2

Examples

switch S := S1, S2, Q[m], if v > — 5 then S3 else S4 switch Q := pl, w 5.3.3

Semantics

A switch declaration defines the set of values of the corresponding switch designators. These values are given one by one as the values of the designational expressions entered in the switch list. With each of these designational expressions there is associated a positive integer, 1,2... , obtained by counting the items in the list from left to right. The value of the switch designator corresponding to a given value of the subscript expression (cf. section 3.5 Designational expressions) is the value of the designational expression in the switch list having this given value as its associated integer. 5.3.4

Evaluation of expressions

in the switch list

An expression in the switch list will be evaluated every time the item of the list in which the expression occurs is referred to, using the current values of all variables involved. 5.3.5

Influence of scopes

If a switch designator occurs outside the scope of a quantity entering into a designational expression in the switch list, and an evaluation of this switch designator selects this designational expression, then the conflicts between the identifiers for the quantities in this expression and the identifiers whose declarations are valid at the place of the switch designator will be avoided through suitable systematic changes of the latter identifiers. 5.4

Procedure

5.4.1

declarations

Syntax

:: = < identifier> < formal parameter list> :: = | < formal parameter list > :: = | () < identifier list > :: = | < identifier list> , < identifier>

:: = value < identifier list> ; |

< specifier> :: = string | procedure

| array | array

| procedure

< specification part> :: =

| label | switch |

| < identifier list> ; |

< specification part > < specifier> < identifier list> Pe < procedure heading> :: = < procedure identifier > < specification part> :: = | < procedure declaration> :: = procedure < procedure body > | procedure < procedure body > 3.4,2

Examples

(see also the examples at the end of the report)

procedure Spur (a) Order: (n) Results: (s) ; value n ;

114

;

array a; integer n ;real s; begin integer k ; Sr

0%

for k := 1 step 1 until ndo s := s + alk,k] end

procedure Transpose (a) Order: (n) ; value n ; array a ; integer n; begin real w; integer i, k; for i := 1 step 1 until n do k := 1 + i step 1 until n do for

begin w := a{i,k] ; ali, Khs= alk, ili; alk i= w end end Transpose

integer procedure Step(u) ; real u ; Step := if 0