Contents / Reference section / BASIC keywords / Previous letter / Next letter / Index


O

OLD

Purpose

To recover a program which has been recently deleted by NEW or by pressing the BREAK key. Programs can only be recovered if no program lines have been entered and if no new variables have been created since the program was deleted. If you get the message Bad program, then type NEW again.

Typing NEW or pressing BREAK are quite drastic moves. OLD will do its best to recover your program but will not always succeed fully. In particular if the first line number in your program is greater than 255 then it will get that one line number wrong. The ESCAPE key provides a clean and tidy method of stopping a program. BREAK is much more violent and should be avoided.

Example

OLD

Description

A command which undoes the effect of NEW.

Syntax

OLD

Associated keywords

NEW

ON

Purpose

To alter the order in which BASIC executes a program by jumping to one of a selection of lines depending on the value of I a particular variable. The word ON is used with three other keywords GOTO, GOSUB and ERROR. For example

ON value GOTO 800,920,100,1170 7300

ON result GOSUB 8000,8300,120,7600

ON ERROR GOTO 9000

ON ERROR GOSUB 2001

First:

ON X GOTO 1100,1210,1450,1600,1950

If the value X is equal to 1 then the program will go to line 1100. If X=2 then the program will go to line 1210. If X=3 then line 1450 and so on.

What is it used for? Suppose that you are counting coins put into a machine and you want to offer different things if 1, 2 or 3 coins are put in. The program which follows illustrates, in outline, how ON GOTO will help.

450 REM the variable COINS gives the number

460 REM of coins inserted

500 ON COINS GOTO 550,600,650

550 PRINT "One coin buys a biscuit"

560 REM give him a biscuit somehow

590 GOTO 1000

600 PRINT "Two coins can buy tea or coffee"

610 GOTO 1000

650 PRINT "Three coins can buy a piece of cake"

660 REM something else in here as well

690 GOTO 1000

1000 REM all the routines end up here

Secondly:

ON X GOSUB 2200 2300 2400 2500 ON can also be used with GOSUB instead of GOTO. See the page describing GOSUB for an explanation of subroutines.

ON X GOSUB provides a neat way of using different subroutines in different situations.

Note: An ELSE clause can be included at the end of ON GOTO and ON GOSUB to trap out-of-range values without causing an error. Unfortunately this facility upsets returning from functions and procedures in the first version of BASIC.

Thirdly:

ON ERROR GOTO

ON ERROR OFF

If the computer detects an error in your program or in the disc drives or anything else that it can't cope with, then it "produces an error". In other words it complains and stops. The complaint takes the form of a message on the screen - for example Too big.

Sometimes it is vital that the computer looks after such situations without troubling the user. The statement ON ERROR GOTO 7000 ensures that if an error occurs the computer does not complain and does not stop. Instead it goes to a piece of program at line 7000 (in this case) which has been specially written to get the computer out of the mess it is in. This section of program may have to give the user instructions like "Please enter a smaller number" or it may be able to sort out the problem in some other way.

How well this "error trapping" works depends on the skill of the programmer in thinking of every possible thing that can go wrong. You will soon re-discover Murphy's Law:

"If a thing can go wrong, it will."

Good error handling is vital in all programs for use by non-specialists - and that means most people!

The statement ON ERROR OFF lets the computer deal with errors once again - cancelling the effect of ON ERROR GOTO.

Examples

40 ON ERROR GOTO 9000

50 ON ERROR PRINT "The computer is confused"

10 ON ERROR GOSUB 2000

Description

A statement providing multiple options in changing the order of execution of a program, and error trapping

Syntax

ON <num-var> GOTO<numeric>{,<numeric>}

or

ON<num-var>GOSUB<numeric>{,<numeric>}

or

ON ERROR <statement>

or

ON ERROR OFF

Associated keywords

GOTO, GOSUB

OPENIN open file for input to computer (from cassette or disc)

Purpose

To tell the computer that your program wishes to read data (words and numbers) from the cassette or disc. Reading data in from cassette or disc is quite a complicated procedure for the computer and it needs advance warning when you wish to do so. The advance warning is given by the OPENIN keyword.

One use of this facility is to store names and addresses on "file" (i.e. the cassette or disc) and to read the file in each time you want to update it. After you have corrected it you can then transfer it back to cassette or disc where it will be saved for future use. Further information about cassette, disc and network "files" is provided on page 188.

A typical example of the use of OPENIN is

X=OPENIN("cinemas")

This informs the computer that you will shortly want to read data in from a file which is recorded on cassette or disc under the name "cinemas". The "filename" is "cinemas".

In accepting this instruction the computer allocates a "channel" to this operation. It is as if it said "O.K. that information will be provided on telephone number 6". It makes X=6 (or whatever number it decides). In all future operations on that file you must refer to it as channel X (channel 6 in this example).

You get the actual data into the computer (from the cassette) by using INPUT#X as the demonstration program on the next page indicates.

Example

230 file=OPENIN("census")

Description

A function which attempts to open a file for input or random access. In a disc or network environment then if a file already exists with the correct name it will be opened for updating (reading or writing).

The function returns the channel number allocated by the computer's file system. If the file does not exist then zero is returned.

Syntax

<num-var>=OPENIN(<string>)

Associated keywords

OPENOUT, EXT#, PTR#, INPUT#, PRINT#, BGET#, BPUT#, EOF#, CLOSE#

Demonstration program

10 REM to read in the names of 10 cinemas from

20 REM cassette assuming of course that you put

30 REM them there sometime before!

50 REM dimension a string array of 10 slots

60 DIM cine$(10)

90 REM open the file

100 channel=OPENIN ("CINEMA")

110 REM and read in the ten cinema names

120 FOR X=1 TO 10

130 INPUT# channel,cine$(X)

140 NEXT X

150 REM that's the information in

160 REM do whatever you want with it!

OPENOUT open file for output to cassette or disc

Purpose

This opens a cassette or disc file for output. Before you can record data (rather than programs) onto a cassette you have to "open a file". More information about "files" is given on page 188.

OPENOUT is used to inform the computer that you wish to record data on cassette or disc. The computer allocates a channel to the operation.

When working with discs or over the network then if a file already exists with that name it will be deleted. If no file exists then a new one will be created.

Example

330 X=OPENOUT ("cinemas")

Description

A function which returns the channel number allocated to an output file.

If a file of the same name exists then that file will first be deleted. If no file exists then one will be created.

Syntax

<num-var >=OPENOUT(<string>)

Associated keywords

OPENIN, PTR#, EXT# INPUT#, PRINT#, BGET#, BPUT#,EOF#, CLOSE#

OPT option

Purpose

This statement determines what output is produced on the screen when assembly language routines are processed by the BASIC interpreter. An understanding of the operation of assemblers is required to understand the following.

During assembly two common errors can occur: "Branch out of range" and "Unknown label".

The latter will occur during pass one for all forward references. It is therefore often desirable to turn off assembler error messages during pass one.

The statement OPT is followed by a number in the range 0 to 3, with the following results;

0 assembler errors supressed, no listing

1 assembler errors supressed, listing

2 assembler errors reported, no listing

3 assembler errors reported, listing

The OPT statement can only occur inside the square brackets which enclose a piece of assembly language. OPT is set to 3 every time the BASIC interpreter finds a [. Do not confuse it with *OPT which is described on page 434.

Examples

200 OPT 1

350 OPT (pass*2+list)

Description

An assembler pseudo-operation controlling the output during assembly. OPT is followed by an expression as detailed above.

Syntax

OPT <numeric>

Demonstration program

10 oswrch=&FFEE

20 DIM memory% 100

30 FOR Z=0 TO 3 STEP 3

35 P%=memory%

40 [OPTZ

50 .start LDA#ASC"!"

60 LDX #40

70 .loop JSR oswrch

80 dex:BNE loop

90 rts:] NEXT Z

100 CALL start

110 END

OR

Purpose

To enable one condition or another condition to determine what happens next.

The OR operator can be used either as a "logical or" or as a "bitwise or". See the keyword AND on page 205 for details of logical and bitwise operators.

Example

75 IF X=6 OR date>20 THEN PRINT "Good"

Description

An operator performing bitwise integer logical OR between two numerics.

Syntax

<num-var>=<numeric>OR<numeric>

Associated keywords

AND, EOR, NOT

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001