# Chapter two: Simple Programming¶

## Arithmetic¶

### Variables and constants¶

The BASIC language can be used readily for calculations. The value of 1 + 2 may be calculated by the statement:

10 LΕΤ A = 1 + 2


The letter A is known as a numeric variable. It can be set to the value of any number. The values 1 and 2 are known as numeric constants.

A numeric constant can be expressed as an integer, a decimal fraction, a mixed number, an integer plus exponent, a fraction plus exponent, or a mixed number plus exponent.

In exponent form A * 10x is written AEX.

e.g:

20 LΕΤ A = 1027
30 LET B = .1027
40 LET C = 10.27
50 LET D = 102E7
60 LET E = 10.2E–7


Any letter can be used as a numeric variable. Numeric variables can also be given names consisting of a letter followed by a single digit in the range 0-9. Thus the full range is A–Z and A0–Z9.

Numeric variables can be used in calculations.

e.g.:

10 LET B = 1
20 LET A = B - 2


All the normal arithmetic operations can be performed.

1. ** or ↑ is the symbol for exponentiation
2. / and * are the symbols for division and multiplication
3. - and + are the symbols for subtraction and addition
4. ΜΑΧ/ΜΙΝ

and are executed in the above order if a number of operations occur in any one expression, or from left to right if equal priority is shown.

e.g.:

30 LET C = 1 + 2 * 3 - 4/8 - 3 ** 2


Sets C = 1 + 6 - 0.5 + 9 = 15.5

The operators MAX and MIN may also be used. They function in the following way:

A MAX B will access the larger of the 2 variables A and B.

A MIN B will access the smaller of the 2 variables A and B

e.g:

20 LΕΤ A ΜΑΧ B = C


Brackets can be used, as in arithmetic, to change the order in which the expression is evaluated.

e.g.:

30 LET C = (((1 + 2) * 3 - 4)/8 + 3 ** 2) * 3

 Sets C = (((3) * 3 - 4)/8 + 9) * 3 = (5/8 + 9) * 3 = 28.875

### Arrays¶

Variables may be grouped together to form an array so that instead of using several variable names one subscripted variable can be used

e.g:

A, B, C, D, E can be replaced by
A(0), A(1), A(2), A(3), A(4)


The bracketed number, which must be an integer, is the subscript. Each subscripted variable is called an element of the array.

Arrays are declared using the DIMENSION statement. This shows how many elements are required and how many dimensions (1 or 2). A two dimensional array (or matrix) could be shown pictorially thus:

A(0,0) A(0,1) A(0,2) A(0,3)
A(1,0) A(1,1) A(1,2) A(1,3)
A(2,0) A(2,1) A(2,2) A(2,3)


and would be declared thus:

DIM A(2,3)


Showing a two dimensional array with the highest first subscript equal to 2 and the highest second subscript equal to 3. Since the lowest subscript is 0 this means that 3 x 4 = 12 elements have been declared.

Note that the first subscript shows the row and the second the column in which the element appears.

Examples

10 DIM P(4)


will declare a one dimensional, 5 element array.

20 DIM B (3,3), C(2,4)


will declare 2 two dimensional arrays one with 16 elements and one with 15 elements.

### Standard functions¶

A set of standard functions is available to BASIC programmers to use in calculations.

These are:

 SIN (X) The sine of X COS (X) The cosine of X TAN (X) The tangent of X ATN (X) The arctangent of X CPI The value of π 3.14159265359 LOG (X) The natural logarithm of X (X>0) EXP (X) ex where e = 2.71828.18285 SOR (X) The square root of X (X⩾0) ABS (X) The absolute value of X INT (X) The largest integer not greater than X SGN (X) The sign of X. If X is positive this is set to +1, if X is zero this is set to zero, if X is negative this is set to -1. EPS An estimate of the precision of arithmetic (about 7.28E–12) i.e. The smallest value such that 1–EPS < 1 < 1 + EPS INF The largest numeric value allowed (about 5E76) RND The next value from the random number generator.

Note: All angles must be in Radians.

These numeric functions can be used as shown

100 LET A = INT ( CPI * R ** 2)
120 LET C = SIN (A + B/2)
130 LET E = F + SOR(27 – P) - RND


### The random number generator¶

The random number generator in the system is initially set to yield a sequence of randomly selected numbers. It will, however, always give the same sequence of random numbers unless the programmer resets it using the statement RANDOMISE.

e.g.:

140 RANDOMISE


A different sequence of random numbers can be produced by giving the random number generator a number to work with.

e.g.:

   150 RANDOMISE 92
Or 160 RANDOMISE B


There are additional standard numeric functions which relate to matrix handling which are described in Chapter seven.

Chapter four describes how user’s own functions can be written.

## Printing¶

The result of a calculation can be printed on the terminal.

e.g.:

10 LΕΤ X = 10
20 LET Y = 20
30 LET Z = X + Y
40 PRINT Z
50 END


When this program is run, the output will simply be the number 30.

It is often helpful to tell the person running the program what the output means.

e.g.:

10 LET X = 10
20 LET Y = 20
30 LET Z = X + Y
40 PRINT "10 AND 20 ARE:"
50 PRINT Z
60 END


This will give:

10 AND 20 ARE:
30
LINE 60 DONE


Spaces and any printable characters except quotes (“) and backarrow (←) can be output by enclosing them within quotes as shown in the example above.

## Program Input¶

### READ, DATA and RESTORE¶

One method of making data available to the program is by storing it using DATA statements and accessing it using READ statements. Use of variables containing strings (e.g. B$) is described in more detail in the next chapter. e.g.: 10 READ A, B$
20 IF A = 99 GOTO 80
30 PRINT B$40 LET C = A*4 50 PRINT C 60 GΟ ΤΟ 10 70 DATA 25, CAR, 15, BUS, 98, TRAIN, 99, AAAA 80 END  Both numbers and strings can therefore be input to the program in this way. The data must be the correct category for the variable into which it is read. If an attempt is made to read a string into area A, the message “INVALID DATA” will appear. DATA statements may be written anywhere in the program, and due to the way the NSP BASIC system is written it is advisable to locate the DATA statements close to the relevant READ statement. The RESTORE statement is used to reposition the data pointer in the program’s DATA statements, if it is necessary to reread some data. If RESTORE is followed by a statement number, the pointer is set to the first DATA statement at or after this number. If RESTORE is written alone, the pointer returns to the start of the first DATA statement. ### The INPUT statement¶ It is often convenient to write a program so that different sets of data can be presented to it when it is being run from the terminal. This can be done using the INPUT command. e.g.: ΝΑΜΕ ΜULΤ 10 INPUT A, C 20 LET B = A + C 30 PRINT B 40 END RUN MULT ? 2,2 4 LINE 40 DONE  When this program is run the program will print out ? followed by a space and then the person running the program can input the numbers they choose, separated by a comma. Output will be produced when the PRINT statement is encountered. It is a good practice to output a message to the person running the program so that they know what kind of input is expected. It is also a good practice to check what has been typed in case the person running the program makes an error. Character strings may also be input in this way. If non-numeric characters are typed and the variable is not labelled with a$ or a £ sign, the program will halt with the message:

linenumber BAD INPUT RETYPE FROM ITEM 1


if the first item was wrong. The question mark and space will appear again and an appropriate number can be input.

### The LINPUT statement¶

This statement allows input of whole lines of text from the terminal to a string variable. The statement may be followed by a string-variable list,

e.g.:

20 LINPUT A$, B$, C$ and when the lines of text are input they are assigned in turn to these string variables. ## Simple loops and branches¶ In the simple examples of programs which we have given, the program has started at the first statement, performed each subsequent statement once and ended at the last statement. However, it is often convenient to make the program loop round a sequence of instructions and perhaps halt at some statement other than the last statement. A program run can be halted using a stop statement. However, the END statement must also be present. e.g.: 10 LΕΤ Α = 20 20 LET B = 10 30 LET A = A + B 40 PRINT A 50 IF A = 80 STOP 60 GΟ ΤΟ 30 70 END  This program will print the numbers, 30, 40, 50, 60, 70 and 80. Statement number 30 will give A a new value which is equal to its old value plus the value of B. Every time the statement GO TO 30 is obeyed the program will loop round and execute statements 30, 40 and 50. When A = 80 the program will stop. More complex formats of the IF statement are given in Chapter four. A more efficient way of creating program loops is to use the FOR instruction. ## FOR loops¶ The FOR statement is used to set up a loop. It is in the form FOR X = A TO B STEP C e.g.: 100 FOR X = 1 TO 10 STEP 2 110 PRINT X 120 ΝΕΧΤ X  X is initially set to 1. It is then tested to see whether it is greater than 10, and if not it is printed. The ‘NEXT’ statement is then encountered and X is set to the next value, i.e. 1 + 2 = 3, and so on. X will increase in steps of 2 until the test X > 10 gives the answer true, i.e. when X = 11 control will then pass to the statement after the NEXT. This example will thus print 1 3 5 7 9 If the STEP is 1 the “STEP C” part may be omitted. e.g: 100 FΟR X = 1 ΤΟ 10  is equivalent to: 100 FOR X = 1 TO 10 STEP 1  ## Running programs¶ The current program will be executed following the command RUN which will start the program at the first line or RUN statement number which will start the program at the given line. e.g.: RUN 40  will start the program at line 40. More complex formats of the RUN command are given in Chapter eight. The program run can be stopped in two ways: press “A” while holding the “control” key down - during output or processing (CTRL and 1 if using an ICL VT). press “Z” while holding the “control” key down and then “Return” - in reply to a request from an INPUT statement. ## Changing the current program¶ ### Adding additional lines and changing lines¶ The program currently held in store may be altered in several ways. Additional lines may be added by typing in the line numbers and BASIC program statements as normal. e.g. If the current program was: 10 REM MY PROG 20 ΡRΙΝΤ "ΑΝΥΤΗΙΝG ΑΤ ΑLL" 30 END  and the following lines were entered: 10 REM MY NEW PROG 25 PRINT “ADDITIONAL LINE”  the current program would then consist of the following: 10 REM MY NEW PROG 20 PRINT “ANYTHING AT ALL" 25 PRINT “ADDITIONAL LINE” 30 END  Note that the old line 10 has been overwritten by the new. A stored program may be added to the end of the current program using the APPEND command. e.g: APPEND anyprog - this would add the program anyprog onto the end of the current program. Note: The line numbers used in the appended program must have line numbers greater than those in the current program. ### Deleting lines¶ A line may be deleted by entering its line number only, before pressing the ACCEPT key. Alternatively, one or more lines may be deleted with the DELETE command. This has the same series of formats as the LIST command (see below). e.g.: DELETE stno, stno - this would delete all the statements of current program within the given range, inclusive (stno = statement number) ## Inspecting the current program¶ Your current program may be inspected by means of the LIST command which has four forms:  LIST lists all of the current program LIST stno lists the given statement of the current program LIST stno, lists the current program from the given statement to the end LIST stno, stno lists all of the current program between the given limits inclusive. LIST , stno lists the current program from the beginning to the given statement. e.g.: LIST 20 20 ΡRΙΝΤ "ΑΝΥΤΗΙΝG ΑΤ ΑLL''  ## Naming a program in store¶ Before a new program is stored on disc, it must be named. The command NAME may be used for this purpose. e.g.: ΝΑΜΕ FRΕD  When this is typed and “accept” is pressed, any BASIC lines that are already in store are named “FRED”. Lines may be added or amended and these alterations will be included in the named program. An alternative to NAME is the NEW command. e.g.: NEW FRED  This command will delete any BASIC lines already in store when it is entered so that only lines typed after the command are included in the named program. ## Storing programs on disc¶ Programs held in store may be stored on disc for use at a later time. This is done as follows:  SAVE This will open a new file on disc with the name of the current program and save the current program in it. Users logged on under other usernames cannot access this program. SAVE RUN As above but other users can GET and RUN the program. SAVE SHARE As above but other users can GET, RUN, LIST and copy the program. e.g.: SAVE SAVE RUN  Note: 1. Programs to be stored on disc must be named. 2. To store an edited program under the original name, the original stored program must first be deleted. ## Listing details of stored programs¶ Details of all user files currently stored on disc are available to the user. The commands to obtain these details are:  CATALOGUE lists the names and types of all user files CATALOGUE .id lists the names and types of the sub-users files. More complete details may be obtained by typing  CATALOGUE FULL Or CATALOGUE .id/FULL  which will produce a table showing file name, file type (e.g. B = BASIC program file, I = internal format file, D = terminal format file), date last used, number of buckets taken and access (e.g. Share, User, Write, Run). (A bucket is a unit of storage on disc). In addition to users’ files, files of general library programs available to all users can be accessed. The names and type of all library files can be obtained by the command: LIBRARY  Unless access is prevented it is possible to list the catalogue of other users. e.g.: LIBRARY FRED  where FRED is the username for the catalogue to be listed. e.g.: LIBRARY LIBRARY CLASS3 CATALOGUE .JP/FULL  ## Retrieving stored programs¶ A file which has previously been saved on disc may be run or edited when it has been loaded into store with one of the commands:  GET name Or OLD name  where name is the program name under which it was stored. e.g.: GET FRED  To load a library program for use, a “$” or “£” sign must be typed before the program паmе.

e.g:

GET \$LIBPRO


If another user’s files are not restricted they may also be accessed.

e.g:

GET TEST, LORENS


where TEST is the name of the program to be loaded and LORENS is the user-name under which the program is stored.

## Deleting stored programs¶

The following commands may be used to delete unwanted stored programs:

   KILL name
Or UNSAVE name


where name is the name by which the program is stored.

Once these programs have been deleted they can not be recovered.

e.g.:

KILL FRED will delete the program named FRED from the user’s disc file.

The current program may be deleted from store by means of the SCRATCH command

e.g.:

GET FRED
SCRΑΤΟΗ


This will cause the program FRED to be loaded into store and then deleted from store.

Note:

All commands except HELP may be abbreviated to their first three letters.

e.g.:

CΑΤ
LIB