Introducing BBC BASIC
Introducing BBC BASIC
BBC BASIC
R. B. Coats
Introducing
BBC BASIC
R.B. Coats
Principal Lecturer in Computer Studies
Leicester Polytechnic
Edward Arnold (Publishers) Ltd. hereby warrant that this book is in no way
connected with either the BBC or the manufacturers of the computer, Acorn.
Edward Arnold
R. B. Coats 1984
Edward Arnold, 300 North Charles Street, Baltimore, Maryland 21201, U.S.A.
Edward Arnold (Australia) Pty Ltd, 80 Waverley Road, Caulfield East, Victoria 3145, Australia
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, electronic, mechanical, photocopying, recording or
otherwise, without the prior permission of Edward Arnold (Publishers) Ltd.
Printed in Great Britain by Butler & Tanner Ltd, Frome and London
Preface
explanatory text;
test questions;
practical exercises.
The majority of units are short enough for the explanatory text
and the test questions to be covered in one lesson. The practical
exercises may be undertaken individually by pupils on a computer
if sufficient access to computers is available. Alternatively,
the teacher may demonstrate these exercises on a single computer
in front of the class. Those practical exercises which involve
writing programs should be tackled by each pupil first writing
his or her answer on paper. These answers can then by tried on a
computer. Again, if sufficient access to computers is available,
each pupil can type his/her own program into a computer and run
it. Otherwise, the teacher can select one or two of the pupils'
answers, and run those on a single computer in front of the whole
class. Constructive criticism of programs written by others is a
valuable part of learning to program.
iv Preface
Preface iii
1 Introduction 1
2 Variables 7
3 What is a program? 14
4 Input and output 19
5 Looking after your programs 26
6 Editing 35
7 REPEAT loops 39
8 Strings 44
9 Graphics 49
10 Colour 55
11 FOR loops 59
12 READ and DATA 67
13 Numbers 71
14 Sound 76
15 Timing 81
16 The IF command 86
17 Procedures 94
APPENDICES
A Answers 102
B Summary of BASIC 105
C Additional Questions 108
D Answers to Additional Questions 116
Index 120
1 Introduction
1.2 Computers
Type
PRINT 3+1 and then press RETURN.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
PRINT 5+2
PRINT 6+3
PRINT 7-4
PRINT 10-5
Did you remember to press the RETURN key each time? Note that '-'
is on the same key as '=', and means subtract (take). Check that
the computer's answers are correct.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
PINT 3+1
Note that PRINT has been spelt incorrectly: you missed out the R.
Typing mistakes are common, especially for the beginner.
Fortunately, there is no harm done. The computer only recognises
certain commands, and PINT is not one of them. So it tells you
that you have made a mistake.
If you realise that you have made a mistake BEFORE you press
the RETURN key, then you can correct it. If you press the DELETE
key once, the last character you typed is deleted. If you press
it again, another character is deleted. And so on.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
PINT but don't press the RETURN key.
Now type
The command should be executed correctly. From now on, you won't
be reminded to press RETURN after each command. This is something
you will have to remember for yourself.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
CALCULATE 3+1
DISPLAY 3+1
Although the English looks correct, the computer does not know
the meaning of the words CALCULATE and DISPLAY, and so thinks you
have made a mistake. The computer will only accept words it
knows, and the one for displaying something on the screen is
PRINT.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The computer can also perform multiplication (times) and division
(divides).
Type
PRINT 2*4
PRINT 5*5
PRINT 8/2
PRINT 9/3
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This exercise shows the effect of inserting spaces in a command.
Type
PRINT2*3
PRINT 2*3
PRINT 2 * 3
PRIN T 2*3
1.3 The PRINT command 5
All these are valid commands except the last one. Spaces can be
inserted between words and numbers, but it is wrong to insert
spaces between the letters making up a word: PRIN T is incorrect
because of the space between the N and the T.
You should always put a space after a command word. For
example:
PRINT6/2
IFAGE<11THENPRINT"CHILD"
Questions
5. What is a command?
9. What happens when you press the RETURN key after typing in a
command?
11. Work out what the computer will do with these commands.
Write your answers in the 'your answer' column.
PRINT 5+3
PRINT 5-3
PRINT 2*3
PRINT 15/5
PING 2+3
PRINT 5 + 3
CALCULATE 5+3
PRI NT 5+3
PRINT 4+3+2
PRINT 4+3-2
Now type the commands into the computer, and see if you were
right.
12 9
Each box can hold one piece of information. At this stage we will
only consider numbers being stored in the boxes, but in later
units we will see that other information (this book, for example)
can be stored as well. In the diagram above, the third box from
the left contains the number 12, and the fifth box from the left
contains the number 9.
Suppose you want to tell the computer about the age of a boy
called DAVID. This can be done by typing the command
LET DAVIDSAGE=12
12
DAVIDS
AGE
LET DAVIDSAGE=12
PRINT DAVIDSAGE
and see what happens. Success! The computer will display the
contents of the named box.
A name such as DAVIDSAGE is called a variable. It is said to
be 'variable' because the contents of its box can be altered. For
example, after David's next birthday, you might type
LET DAVIDSAGE=13
The box for DAVIDSAGE already exists, and it currently holds the
number 12. This command simply stores a new number (13) in the
box, and the previous number (12) is forgotten.
The LET command puts a value into one of the boxes making
up the computer's memory.
Type
LET MARTINSAGE=9
PRINT MARTINSAGE
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2.2 The LET command 9
Type
LET MARTINSAGE=10
PRINT MARTINSAGE
The MARTINSAGE memory box should now contain the number 10.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
PRINT ALISONSAGE
The computer should tell you that there is 'no such variable'. A
memory box called ALISONSAGE can only be created by a LET command
such as LET ALISONSAGE=5. Since you haven't typed such a command,
the computer doesn't have a box called ALISONSAGE, and therefore
it cannot display its contents.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
PRINT DAVIDSAGE
NEW
PRINT DAVIDSAGE
The NEW command tells the computer to forget about all the names
that have so far been attached to the boxes, and to start again.
You can think of the computer rubbing off the names. Hence,
following the NEW command, it doesn't know about DAVIDSAGE and
MARTINSAGE, and when you try to use one of these names, the
computer tells you that there is 'no such variable'.
The NEW command rubs off any names that you have attached
to the boxes making up the computer's memory.
Type
LET DAVIDSAGE=10 10
PRINT DAVIDSAGE DAVIDSAGE
The LET command creates a box called DAVIDSAGE, and stores the
number 10 in it.
10 Variables
Type
LET PAYFORDAVID=5*DAVIDSAGE
Type
PRINT PAYFORDAVID 50
PAYFORDAVID
and check that it contains 50.
Type
PRINT DAVIDSAGE
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
LET MARTINSAGE=9 9
PRINT MARTINSAGE MARTINSAGE
Type
LET MARTINSAGE=MARTINSAGE+1
Although this command may look rather odd at first, the computer
processes it in a similar way to the last exercise. Firstly it
gets the value held in MARTINSAGE, which is 9. Then it adds 1,
giving 10. Finally it assigns the value 10 to MARTINSAGE.
Type
PRINT MARTINSAGE 10
MARTINSAGE
and check that it is correct. Note that the previous value (9)
has been lost.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2.2 The LET command 11
The numbers in the boxes below show the values that will be
stored in the computer's memory after the following LET commands
have been executed.
NEW
LET A=2 2 A
LET B=3 3 B
LET C=A+B 5 C
LET C=C-1 4 C
Type these commands into the computer, and check (using PRINT
commands) that the values in the computer's memory boxes agree
with the values above.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Again, the numbers in the boxes below show the values that will
be stored in the computer's memory after the following LET
commands have been executed. However, a deliberate mistake has
been made. Can you spot it?
2 A 3 B 4 C
LET A=A+4 6 A
LET B=A-C 2 B
LET C=B*B 9 C
LET A=A/2 3 A
Type these commands into the computer, and see (using PRINT
commands) whether the computer finds the same mistake as you did.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
12 Variables
GALLONS
AD1984
AGE
Type
LET LETTER=9
LET PRINTER=4
5. What happens if you try to use a variable which has not yet
been given a value?
7. Work out what values will be in the memory boxes after the
computer has processed each of the following commands. Write your
answers in the boxes provided.
LET A=6 A A
LET B=A/2 B B
LET C=A-B C C
LET C=3-C C C
Now type the commands into the computer, and check (using PRINT
commands) that your answers are correct.
3 What is a program?
1 LET AGE=10
2 PRINT AGE
3 LET PAY=5*AGE
4 PRINT PAY
5 END
The program is stored in the computer's memory. You can tell the
computer to execute the commands of the program by typing RUN. In
response, the computer will fetch from memory the command
contained in Line 1 of the program, and execute it. Then the
computer will fetch the command contained in Line 2, and execute
that. This continues until it reaches the end of the program. As
you can see, the line-numbers tell the computer the order in
which the commands are to be executed.
How do we get a program into the computer's memory in the
first place? Easy. Simply type the line-number, followed by the
command, and repeat for each line of the program.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Remembering the line-numbers, type
1 LET AGE=10
2 PRINT AGE
Notice that the computer simply accepts what you type, and then
displays its prompt, waiting for another command. It does not
execute the commands; if it did, you would see the number 10
appear on the screen, resulting from the PRINT AGE command.
3.2 The TRACE command 15
Type
3 LET PAY=5*AGE
4 PRINT PAY
5 END
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
LIST
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
RUN
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
TRACE ON
RUN
16 What is a program?
<1> <2> 10
<3> <4> 50
<5>
The numbers between the < > pairs are the line-numbers. Line 1 is
displayed first because it was executed first; then comes Line 2,
followed by the display of 10 resulting from the PRINT AGE
command; Line 3 is next; then Line 4, followed by the display of
50 resulting from the PRINT PAY command; finally Line 5 is
executed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
TRACE OFF
This tells the computer to stop tracing. Try the RUN command
again, and confirm that the computer is no longer printing line-
numbers.
Type
NEW
LIST
Type
5 END
2 PRINT AGE
1 LET AGE=10
3.3 The AUTO command 17
and list the program. Even though we typed the program in reverse
order, the computer stores the program in its memory in line-
number order.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
3 LET PAY=5*AGE
4 PRINT PAY
LIST
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
NEW
AUTO
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
LIST
Type
TRACE ON
RUN
TRACE OFF
Notice that the computer executes the line with the smallest
line-number (10) first, then the line with the next smallest
line-number (20), and so on.
1. What is a program?
10 LET AGE=10
20 PRINT AGE
30 LET PAY=5*AGE
40 PRINT PAY
50 END
PRINT 2+3 : the computer works out 2 add 3, and then displays
the answer on the screen.
PRINT AGE : the computer displays on the screen the number
stored in the memory box called AGE.
Type
PRINT "HELLO"
Type
PRINT "AGE"
LET AGE=10
PRINT AGE
20 Input and output
The command PRINT "AGE" tells the computer to display the word
AGE. The command PRINT AGE tells the computer to display the
contents of the box called AGE (10 in this case). It is important
that you understand the difference between these two cases.
Using AUTO, type the following program into the computer:
10 PRINT "A"
20 PRINT "B";
30 PRINT "C"
40 PRINT "D"
50 END
Did you notice the semi-colon in Line 20? If not, retype the
line. Now run the program. You should see the following output:
A
BC
D
The cursor is the name given to the marker that the computer
displays on the screen to show you where the next character will
appear. Cursors vary from computer to computer - on the BBC
computer it is a flashing underline character.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
What output would you expect this program to produce?
10 PRINT "123";
20 PRINT "45"
30 PRINT "6";
40 PRINT "78"
50 END
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter into the computer the pocket money program shown at the
start of this unit (don't forget to type NEW before you start).
Type
15 PRINT "AGE = ";
35 PRINT "POCKET MONEY = ";
LIST
You have inserted two extra lines into the program. Now run the
program. The output should be:
AGE = 10
POCKET MONEY = 50
Is the output as you would expect? Notice that both Line 15 and
Line 35 have a semi-colon at the end of the line.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
15 PRINT "AGE = "
35 PRINT "POCKET MONEY = "
LIST
RUN
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Up to now we have only used the PRINT command with one item, for
example PRINT AGE. BASIC allows us to include a number of items
in one PRINT command; the items are separated by semi-colons.
When the command is executed by the computer, all the items will
be displayed on one line.
Type
PRINT "ABC" ; "123" ; "DE" ; "45"
22 Input and output
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
LET AGE=10
PRINT "AGE = ";AGE
and you should see AGE = 10 displayed on the screen. Notice that
there are two items in the PRINT command ("AGE = " and AGE).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Our pocket money program now becomes:
10 LET AGE=10
20 PRINT "AGE = ";AGE
30 LET PAY=5*AGE
40 PRINT "POCKET MONEY = ";PAY
50 END
Our pocket money program works well for a boy or girl who is aged
10. Suppose, however, that we want to work out the pocket money
for David, who is 12 years old. As it stands, the program will
not work. We must change Line 10 to LET AGE=12, and then run the
program.
Type
10 LET AGE=12
LIST
RUN
Type
10 INPUT AGE
LIST
RUN
4.2 The INPUT command 23
Type
10 in response to the ?, and press the RETURN key.
Type
RUN
and 5 in response to the ?, and press the RETURN key.
Has the computer produced the correct answer? We can now run the
program as often as we like, and obtain the pocket money for
whatever age we enter.
When the computer executes the INPUT command in the pocket money
program, and it displays the ? on the screen, we know that the
computer is asking us to enter the child's age because we are
familiar with the program. However, someone not familiar with the
program is unlikely to know this. The problem is the same as we
met when dealing with output (the numbers 10 and 50), and it can
be overcome in exactly the same way.
Type
5 PRINT "ENTER AGE ";
LIST
RUN
10 PRINT "A"
20 PRINT "BB";
30 PRINT "CCC"
40 PRINT "DDDD"
50 END
10 PRINT "A";"B";
20 PRINT "C";"D"
30 PRINT "E";"F"
40 END
7. How can you stop the cursor moving to the next line after a
PRINT command is executed?
Hints:
a) you will need another INPUT command between Line 10 and Line
20, asking for the weekly amount. Use a variable called
WEEKLY.
b) you will need to replace Line 20 by a line that calculates
PAY by multiplying WEEKLY by AGE, rather than 5 by AGE.
Type the program into the computer, and then run it. When the
program has run, the screen should look something like:
Type
LIST
RUN
Does the program work correctly? Now switch off your computer,
and then switch it back on again.
Type
LIST
You should find that the program has disappeared. If you want to
run this program again, you will have to re-type it all.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Many of the units in this book require you to save programs on a
cassette. One C15 cassette will hold all the programs contained
in this book, so get a C15 cassette.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Every cassette should be given a name which reflects the purpose
of the programs on that cassette. A suitable name for our
cassette might be UNIT PROGRAMS, reflecting the fact that the
programs on it are the programs associated with the units of this
book. If your cassette has a label already, then write UNIT
PROGRAMS on this label. If not, then get a sticky label, write
UNIT PROGRAMS on it, and stick the label to the cassette.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Insert the cassette into your recorder, with Side 1 uppermost.
Rewind the tape, and set the tape counter to 000 (if your
recorder has one). Press the PLAY button on the recorder, and let
28 Looking after your programs
the tape wind forward until the tape counter reads 003. Take the
cassette out of the recorder. The 'leader' should have been wound
onto the right-hand spool, and you should see just brown tape. If
the leader is still visible, you will have to wind it a little
further. Put the cassette back into the recorder. It is now ready
for recording programs.
You will need to give a name to each program that you save on
tape. A maximum of 10 characters is allowed in the name if you
are saving programs on cassette, and a maximum of 7 characters if
you are saving programs on disk. This book uses names of 7
characters or less, so that the programs may be saved on either
cassette or disk without having to alter the names used.
Choose sensible names such as:
"POCKET" for our 'pocket money' program. Note that the name
has been shortened because there are 11 letters in
the words 'pocket money', which exceeds the
maximum we are using (7).
People modify programs after they have been written, and often
keep several versions of the same program. If you use 6
characters or less for the program name, then there will be a
spare character which can be used for the version number. For
example, "POCKET1" for version 1 of the pocket money program,
"POCKET2" for version 2, and so on.
It is good practice to start every program with a title.
With its title, the pocket money program becomes:
10 REM POCKET1
20
30 INPUT "ENTER AGE ",AGE
40 LET PAY=5*AGE
50 PRINT "POCKET MONEY = ";PAY
60 END
Type the pocket money program shown on Page 28 into the computer.
Run it to make sure that it is working correctly.
30 Looking after your programs
The computer will record the program on the tape. When the
computer has finished, the > prompt will appear on the
screen. If your cassette has automatic motor control, then
the tape will stop automatically. If it hasn't, you will
have to stop the recorder manually.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Add the name of the program (POCKET1), and the final reading of
the tape counter, to the 'cassette contents' sheet, as below:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type the following program into your computer:
10 REM CLEVER1
20
30 PRINT "I AM A CLEVER COMPUTER"
40 PRINT "GOOD BYE"
50 END
5.6 The LOAD command 31
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Add the name of the program (CLEVER1), and the final reading of
the tape counter, to the 'cassette contents' sheet, as below:
The shaded areas represent short gaps between the programs. These
gaps are caused by the pause between pressing the RECORD button,
and pressing the RETURN key, when saving programs.
2. Type
LOAD "POCKET1"
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The program is now in the computer's memory. List it, and check
that it is correct. Now run the program. Does it perform
correctly?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
With the UNIT PROGRAMS cassette as it was at the end of the last
exercise, type
LOAD ""
and press the PLAY button on your recorder. The two quotes are
typed one after the other with no space between. If you omit the
program name in the LOAD command, and just type LOAD "", then the
computer will load the next program on the tape, no matter what
it is called. In this case, CLEVER1 should be loaded.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
List the program, and check that it is correct. Now run the
program. Does it perform correctly?
and press the PLAY button on the recorder. The name of each
program is displayed on the screen as it is encountered on the
tape. No program is loaded into memory.
5.8 Backup
The NEW command clears the computer's memory in readiness for you
typing in your next program. What happens if you accidently type
NEW before you have saved your present program? With many
computers, the program will be lost, and you will have to retype
it all. The BBC computer provide a safety net. If you type OLD
before you have entered any lines of the new program, then the
previous program will be restored to memory.
Questions
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Load the POCKET1 program from your cassette. List it and run, and
make sure that it is working correctly. Suppose we now want to
make the computer appear a little more polite.
Type
30 INPUT "PLEASE ENTER YOUR AGE ",AGE
List and run the program. The previous Line 30 has been replaced
by the line you have just typed in. ANY line can be replaced by
typing a new line with the same line-number.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
55 PRINT
56 PRINT "GOOD BYE"
57 PRINT
List and run the program. A new line can be inserted into a
program by choosing its line-number to be BETWEEN the line-
numbers of the lines above and below the new line. Hence, Lines
55, 56 and 57 are inserted between Line 50 and Line 60. Notice
that a PRINT command by itself (as in Line 55) simply produces a
blank line on the screen when it is executed by the computer.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
RENUMBER
List and run the program. The program should run exactly as
before. Note, however, that the line-numbers have been altered so
that each line-number is 10 more than the previous one. You
should always renumber the line-numbers after inserting lines,
36 Editing
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
RENUMBER 1,1
LIST
RENUMBER 100,100
LIST
RENUMBER 100,10
LIST
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
110
LIST
Notice that Line 110 has been deleted. The simplest way to delete
a single line is to type its line-number, and then press the
RETURN key. Contrast this with inserting a blank line, where we
type the line-number, a SPACE, and then press the RETURN key.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
DELETE 150,170
LIST
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
RENUMBER the program. It should now be:
10 REM POCKET1
20 INPUT "PLEASE ENTER YOUR AGE ",AGE
30 LET PAY=5*AGE
40 PRINT "POCKET MONEY = ";PAY
50 END
6.2 Modifying part of a line 37
To replace a line: type the new line with the same line-
number as the line to be replaced.
When you find an error in a line of your program, you can correct
it by retyping the whole line. The new line will replace the
previous line if it is given the same line-number. The BBC
computer provides a means by which you can correct PART of a line
without having to retype the WHOLE line.
screen
DELETE COPY
20 PRINT "GOO scratchpad
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use 'edit mode' on your computer to change the following lines of
the pocket money program:
Questions
repeat
a group of commands
until told to stop
10 REPEAT
20 INPUT "ENTER AGE ",AGE
30 PAY=5*AGE
40 PRINT "POCKET MONEY = ";PAY
50 UNTIL told to stop
60 END
50 UNTIL AGE=0
Notice that we have typed two spaces at the start of Lines 20, 30
and 40. This is known as indentation. We indent the group of
commands between the REPEAT and the UNTIL so as to make the
program easier to read. if there are several REPEAT commands in a
40 REPEAT loops
program, and you are not using indentation, then it isn't always
easy to see which UNTIL belongs to a particular REPEAT. With
indentation it is obvious.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
10 REPEAT
20 INPUT "ENTER AGE ",AGE
30 PAY=5*AGE
40 PRINT "POCKET MONEY = ";PAY
50 UNTIL AGE=0
60 END
List and run the program. Try it with values of 5, 12, 7 and 0.
You should be asked to ENTER AGE four times, and then the program
should stop running. Does it work correctly?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
45 ..PRINT (replacing the two dots by two spaces).
List and run the program. Try it again with values of 5, 12, 7
and 0. It should produce the same results as before, but they
should be displayed more clearly on the screen because of the
blank line between each set of results (produced by the computer
executing Line 45). You should always try to make your results as
clear as possible.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
TRACE ON
RUN
The computer executes Line 10, and then waits at Line 20 for you
to enter an age.
Type
5
The computer stores this value in the memory box called AGE.
REPEAT loops 41
AGE
The computer executes Line 30, followed by Line 40, which causes
POCKET MONEY = 25 to be displayed on the screen. Then it executes
Line 45, producing a blank line on the screen.
Type
0
The computer stores this value of 0 in the memory box called AGE.
AGE
When the computer comes to Line 50, it gets the value contained
in the AGE memory box (0 this time) and says 'is this equal to
0?'. It is. The computer has repeated the commands UNTIL AGE=0,
and it is now time to stop the repetition. So the computer goes
on to Line 60, which ends the program.
42 REPEAT loops
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
TRACE OFF
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Modify the pocket money program to become:
10 REM POCKET2
20
30 REPEAT
40 INPUT "ENTER AGE ",AGE
50 PAY=5*AGE
60 PRINT "POCKET MONEY = ";PAY
70 PRINT
80 UNTIL AGE=0
90 END
List and run the program. Make sure that it is working correctly.
Now save the program on your UNIT PROGRAMS cassette as POCKET2.
Don't forget to fill in the 'cassette contents' sheet.
Questions 43
4. What is meant by the word indented? Why should the loop body
be indented?
set bill to 0
repeat
input item
add item to bill
until item=0
print bill
end
Translate this into a BASIC program, and use variable names ITEM
and BILL. List the program to check that it is correct, and then
run it with the following data:
will display the message 'POCKET MONEY = ' on the screen. The set
of characters between the quotes is called a string. A character
is any of the symbols on the keys of the keyboard. This includes:
The command
LET WORD$="COMPUTER"
COMPUTER
defines a 'string' memory box
called WORD$, and stores the
string "COMPUTER" in it. WORD $
The $ at the end of the variable name tells the computer that
this is a string variable, and distinguishes it from the number
Strings 45
variable of the same name (i.e. WORD). The rules for naming
string variables are the same as those for naming number
variables; the name is chosen, and a $ is then added at the end.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
and run the program. Type your name when invited to do so. The
characters you type are stored in the string variable called
NAME$. The contents of NAME$ are then displayed on the screen by
Line 20.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
LET A$="12"
PRINT A$
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
LET B="12"+3
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
LET A$=3
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
You are now in a position where you can type English words into
the computer, and it can display English words on the screen.
Type the following program into your computer.
46 Strings
10 REM JOKES1
20
30 INPUT "PLEASE TYPE YOUR NAME ",NAME$
40 CLS
50 PRINT "HELLO ";NAME$
60 PRINT
70 PRINT "I AM YOUR CLEVER BBC COMPUTER"
80 PRINT
90 PRINT "********************************"
100 PRINT "WHAT DID THE SEA SAY TO THE SAND";
110 INPUT PAUSE$
120 PRINT
130 PRINT
140 PRINT "NOTHING - IT JUST WAVED"
150 PRINT
160 PRINT
170 PRINT "BET YOU DIDN'T GET THAT ONE, ";NAME$
180 PRINT
190 PRINT
200 PRINT "********************************"
210 PRINT "WHAT WOBBLES AND FLIES";
220 INPUT PAUSE$
230 PRINT
240 PRINT
250 PRINT "A JELLYCOPTER. HA HA"
260 PRINT
270 PRINT
280 PRINT "********************************"
290 PRINT
300 PRINT "BYE FOR NOW"
310 END
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
List the program and check that you have typed it correctly. Now
run the program. Does it perform correctly?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
When the program is working correctly, save it on your UNIT
PROGRAMS cassette as JOKES1. Don't forget to fill in the
'cassette contents' sheet. You can obviously extend this program
by putting in your own jokes.
Strings 47
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
LIST
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
You can set the computer into paging mode so that it stops at the
bottom of each page.
Type
CTRL N (hold down the CTRL key, then press the letter N)
LIST
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press
SHIFT
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
To set the computer back to 'scroll' mode:
Type
CTRL O (hold down the CTRL key, then press the letter O)
LIST
and check that the program scrolls off the top of the page, as it
did previously.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
LIST 100,200 to list all lines between 100 and 200
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
48 Strings
1. What is a string?
2. How many characters are there in the string "XRY 382 T"?
a) LET A$="10"
b) LET A$=10
c) LET A=10
d) LET A="10"
10 REPEAT
20 INPUT NAME$
30 PRINT "I LIKE ";NAME$
40 PRINT
50 UNTIL NAME$="STOP"
60 END
8. What command can you use to make the computer pause waiting
for you to press a key?
10. What is meant by scroll mode? How do you set the computer
into scroll mode?
9.1 Introduction
The BBC computer has EIGHT modes in which it can operate. These
modes are numbered from 0 to 7, and are called Mode 0, Mode 1,...
Mode 7. Only one mode can be used at a time. Each mode has
different capabilities regarding 'text' and 'graphics'. The
differences between the modes are explained in the User Guide
which came with your BBC computer.
We will use Mode 1 for both the graphics in this unit and
the colour in the next unit. Mode 1 is available on a Model B BBC
computer, or a Model A with 32K of memory. If your computer is a
standard Model A, then use Mode 5 instead (i.e. whenever you come
across the command MODE 1, replace it by MODE 5).
50 Graphics
200
XA
y co-ordinate
800
XB
200
0
1279
800 x co-ordinate
1. how far across the screen the dot is. For example, Dot
A is at position 200 across the screen. This is often
called the X co-ordinate of the point.
2. how far up the screen the dot is. For example, Dot A is
at position 800 up the screen. This is often called the
Y co-ordinate of the point.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
10 MODE 1 (MODE 5 on a Model A BBC computer)
20 MOVE 200,800
30 DRAW 900,100
40 END
and run the program. You should see a line on the screen, drawn
from the top left-hand corner towards the bottom right-hand
corner of the screen.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
35 DRAW 900,800
and run the program. You should see an extra line on the screen,
drawn vertically upwards from point (900,100).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
36 DRAW 200,800
and run the program. You should see an extra line on the screen,
drawn horizontally from point (900,800) and completing the
triangle.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
100 200 300 400 500 600 700 800 900 1000 1100 1200
1000 1000
52 Graphics
900 900
800 800
700 700
600 600
500 500
300 300
200 200
100 100
100 200 300 400 500 600 700 800 900 1000 1100 1200
x co-ordinate
9.3 Drawing shapes 53
So far we have seen how the computer can draw a single line using
the MOVE command and the DRAW command together; more complex
shapes can be drawn by adding further DRAW commands. Let us now
get the computer to draw the house shown in Figure 9.1 opposite.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
10 REM HOUSE1
20 MODE 1
30
40 MOVE 200,300
50 DRAW 200,600
60 DRAW 1000,600
70 DRAW 1000,300
80 DRAW 200,300
and run the program. You should see on the screen a rectangle
representing the front wall of the house. Look at the commands
closely, and make sure that you understand how they work.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
90
100 MOVE 200,600
110 DRAW 400,800
120 DRAW 800,800
130 DRAW 1000,600
and run the program. The roof should now be added to the house.
Notice that we have left a blank line between the group of
commands which draws the front wall, and the group of commands
which draws the roof. The blank line makes the program easier to
follow when you make a mistake and have to go back to correct it.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
When the program is working correctly, save it on your UNIT
PROGRAMS cassette as HOUSE1. Don't forget to fill in the
'cassette contents' sheet.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
54 Graphics
Questions
Mode 1 (or Mode 5) has four colours - black, red, yellow and
white. When you first switch to Mode 1, characters are printed in
white on a black background. If the colours are displayed on a
black-and-white screen, they will appear as shades of grey.
Different colours are selected using the COLOUR command.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Load the HOUSE2 program from your UNIT PROGRAMS cassette. Run the
program and check that it is working correctly.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
MODE 1
COLOUR 1
LIST red characters on a black background.
COLOUR 2
LIST yellow characters on a black background.
COLOUR 3
LIST white characters on a black background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
COLOUR 0
LIST
Type
COLOUR 129
LIST
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
COLOUR 130
LIST black on a yellow background.
COLOUR 131
LIST black on a white background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
CLS
The COLOUR command changes the colour of the text foreground and
background. It cannot be used for Graphics. There is a similar
command for graphics, however, and this is the GCOL command. GCOL
stands for Graphics COLour.
10.2 Graphics colour 57
Type
35 GCOL 0,1
and run the program. The lines drawn on the screen should be red.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
35 GCOL 0,2
and run the program. The lines drawn on the screen should be
yellow. GCOL is followed by two numbers. The first is normally 0
(it is beyond the scope of this book to explain the purpose of
the non-zero values of this first number). The second determines
the graphics colour, using the same values as before (1=red,
2=yellow, etc).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
36 GCOL 0,129
37 CLG
and run the program. A background colour can be set by the GCOL
command in much the same way as with the COLOUR command. 129 in
the GCOL command sets the background to red. Hence, in this
example, you should see yellow lines on a red background. The
command in Line 37 clears the screen, and leaves it in the
background colour. CLG stands for CLear Graphics.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
CLG
Questions
We saw in Unit 7 that REPEAT loops are used when you want to
execute a group of commands repeatedly until some condition is
satisfied. For example, you can enter as many ages into the
pocket money program as you like - it makes no difference whether
it is one age, or ten ages, or fifty-seven ages. The computer
keeps on executing the group of commands within the loop until
you enter an age of zero, at which point the program finishes.
There are many instances when you want to execute a group of
commands a fixed number of times. FOR loops are used in this
situation. The structure of a FOR loop is similar to the
structure of a REPEAT loop.
FOR command
NEXT command
The commands between the FOR command and the NEXT command make up
the body of the loop. Notice that the loop body is indented just
as it is in a REPEAT loop.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
10 FOR J=1 TO 5
20 PRINT "HELLO"
30 NEXT J
40 END
The FOR command in Line 10 marks the start of the loop; the NEXT
command in Line 30 marks the end of the loop. The loop body in
this example consists solely of Line 20, which is indented by two
spaces.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Now run the program. The word HELLO should be displayed on the
screen 5 times.
60 FOR loops
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Modify Lines 10 and 30 to become:
10 FOR K=1 TO 5
30 NEXT K
and run the program again. The results should be the same.
The variable used in the FOR command controls how many times
the loop is executed, and so is called the loop-control-variable.
We can use any name for the loop-control-variable, so long as it
is a valid variable name. Many programmers use a single letter
for the name (such as J or K), and we will follow this convention
in the rest of the book.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Modify Line 20 to become:
K = 1
K = 2
K = 3
K = 4
K = 5
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Modify Line 10 to become:
10 FOR K=3 TO 7
and run the program. The starting value of K can be any value: it
11.1 The FOR and NEXT commands 61
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Modify your program to become:
10 FOR K=3 TO 7
20 PRINT "K = ";K
40 END
and run the program. If you omit the NEXT command, as we have
done here, then the K will still be set to 3 by the FOR command,
and the loop body will still be executed. However, as there is no
NEXT command to send the computer back to Line 10, the program
will finish at Line 40, and the loop will have been executed only
once.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
NEW
20 PRINT "HELLO"
30 NEXT K
40 END
and run the program. Here, we have missed out the FOR command. As
there is no FOR command to match the NEXT command at Line 30, the
computer doesn't know which line to go to, and so it tells you
that you have made a mistake.
The FOR command takes the form FOR K=2 TO 5. This tells
the computer to execute the loop with K values of 2, 3, 4
and 5. Hence, the loop will be executed five times.
62 FOR loops
Type
10 KSTART=3
20 FOR K=KSTART TO 7
30 PRINT "K = ";K
40 NEXT K
50 END
and run the program. A variable can be used to give the start-
value; we have used KSTART here.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Modify your program to become:
10 KSTART=3
15 KFINISH=7
20 FOR K=KSTART TO KFINISH
30 PRINT "K = ";K
40 NEXT K
50 END
and run the program. A variable can also be used to give the
finish-value; we have used KFINISH here.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
40 NEXT KSTART
and run the program. The computer will display the error
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Modify your program to become:
10 FOR K=5 TO 3
20 PRINT "K = ";K
30 NEXT K
40 END
and run the program. The loop will be executed once, with K set
to 5. Even though the start-value is greater than the final-value
when the computer first encounters the FOR command, the loop will
still be executed. It is only when the computer gets to the NEXT
command that it compares the K. value with the final-value, and
is able to terminate the loop.
11.2 Using a STEP in the FOR command 63
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Modify your program to become:
and run the program. The loop will be executed with K values of
1, 3, and 5. In other words, K increases in steps of 2.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Change Line 10 to:
and run the program. The loop will be executed with K values of
1, 2, 3, 4, and 5. This is exactly the same as a FOR command of
FOR K=1 TO 5. Hence, if you omit STEP from the FOR command, a
step of 1 is assumed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Change Line 10 to:
and run the program. Notice that minus STEP-values can be used.
In this example, the loop will be executed three times, with K
values of 9, 7, and 5.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
64 FOR loops
Suppose you want the computer to work out and display 'tables'.
The output for the 5-times table might be:
1 TIMES 5 = 5
2 TIMES 5 = 10
3 TIMES 5 = 15
....
10 TIMES 5 = 50
Type
10 REM TABLES1
20
30 INPUT "WHICH TABLE ",TABLE
40 FOR K=1 TO 10
50 LET ANSWER=K*TABLE
60 PRINT K;" TIMES ";TABLE;" = ";ANSWER
70 NEXT K
80 END
Type
5 when invited to enter a table.
1 TIMES 5 = 5
2 TIMES 5 = 10
3 TIMES 5 = 15
....
10 TIMES 5 = 50
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
When the program is working correctly, save it on your UNIT
PROGRAMS cassette as TABLES1. Don't forget to fill in the
'cassette contents' sheet.
Questions 65
10 NUMBER=3
20 FOR K=1 TO NUMBER
30 PRINT K
40 NEXT K
50 END
Type in the program and then run it. Is the output as you expect?
10 KSTART=5
20 KFINISH=9
30 FOR K=KSTART TO KFINISH STEP 2
40 PRINT K
50 NEXT K
60 END
Type in the program and then run it. Is the output as you expect?
4. For each of the FOR commands below, write down in the boxes
the value that K holds each time the loop is executed. The first
has been done for you, to show you what to do:
FOR K=2 TO 3 the 1st time round the loop, K has a value of 2;
the 2nd time round the loop, K has a value of 3.
FOR K=4 TO 2
10 REM SHOP2
20
30 LET BILL=0
40 INPUT "ENTER NUMBER OF ITEMS ",NUMBER
50 PRINT
60 FOR K=1 TO NUMBER
70 PRINT "COST OF ITEM ";K;" = ";
80 INPUT ITEM
90 LET BILL=BILL+ITEM
100 NEXT K
110 PRINT
120 PRINT "TOTAL BILL = ";BILL
130 END
7
3
2
4
1
Type
10 READ A$
20 PRINT "A$ CONTAINS ";A$
30 READ B
40 PRINT "B CONTAINS ";B
50 READ C$
60 PRINT "C$ CONTAINS ";C$
70 DATA "ALPHA"
80 DATA 10
90 DATA "BRAVO"
100 END
The items of data contained in the DATA commands are formed into
a data list by the computer, in the same order as they appear in
the program:
"ALPHA" 10 "BRAVO"
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
70 DATA "ALPHA", 10, "BRAVO"
and delete Line 80 and Line 90. Several data items may be placed
in a single DATA command, so long as they are separated by
commas. Run the program and check that it works.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68 READ and DATA
Type
70 DATA "ALPHA", "BRAVO", 10
and run the program. The string "ALPHA" is assigned to A$, but
when the computer tries to execute the READ B command in Line 30,
it finds that the next item in the data list is the string
"BRAVO". A string cannot be assigned to a 'number' memory box, so
the computer will tell you that there is a 'type mismatch'.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
70 DATA "ALPHA", 10
and run the program. The computer should tell you that it is 'out
of DATA' at Line 50, because you haven't provided sufficient data
items (two data items for three READ commands).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
10 READ A$,B,C$
70 DATA "ALPHA", 10, "BRAVO"
and delete Line 30 and Line 50. Several variable names, separated
by commas, can be included in a single READ command. Run the
program and check that it performs correctly.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The following program asks you to enter the number of a month (1
is January, 2 is February,... 12 is December). The computer then
tells you the name of that month. The program makes use of DATA
commands for storing the names of the months.
Type
NEW
10 REM MONTHS1
20
30 INPUT "MONTH NUMBER = ",M
40 FOR K=1 TO M
50 READ MONTH$
60 NEXT K
70 PRINT "MONTH ";M;" = ";MONTH$
80
90 DATA "JAN", "FEB", "MAR"
100 DATA "APR", "MAY", "JUN"
110 DATA "JUL", "AUG", "SEP"
120 DATA "OCT", "NOV", "DEC"
130 END
READ and DATA 69
Check that you have typed the program correctly. Now run the
program.
Type
1 when asked for the month number.
MONTH 1 = JAN
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Run the program again, and enter 12 when asked for the month
number. Does the computer give the correct answer?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Run the program again, and enter 13 when asked for the month
number. The computer will attempt to execute the FOR loop
thirteen times. As there are only 12 data items in the DATA list,
the computer will report an 'out of DATA' error when it tries to
execute Line 50 for the thirteenth time.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
When the program is working correctly, save it on your UNIT
PROGRAMS cassette as MONTHS1. Don't forget to fill in the
'cassette contents' sheet.
Type
PRINT 3+1
PRINT 5-3
PRINT 2*5
PRINT 15/3
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
PRINT 1+4*3
1 + 4 * 3 becomes
1 + 12 which becomes
13
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
PRINT (1+4)*3
(1 + 4) * 3 becomes
5 * 3 which becomes
15
72 Numbers
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
PRINT 5+12/4
For example:
2 * (1 + (6 - 2) * 2) becomes
2 * (1 + 4 * 2) which becomes
2 * (1 + 8) which becomes
2 * 9 which becomes
18
Type
PRINT 1+2*3
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
PRINT 7-3+2
7 - 3 + 2 becomes
4 + 2 which becomes
Type
PRINT 4/2
PRINT 5/2
PRINT 5/4
PRINT 5/3
The first answer is a whole number. All the other answers are
decimal numbers.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
LET X=4/2
PRINT X
LET X=4/3
PRINT X
Type
PRINT 12345/100
Type
PRINT 12345 DIV 100
DIV performs a divide just as '/' does, but the answer that it
produces is a whole-number. In this example the answer is 123,
because 12345 divided by 100 goes 123 times, with a remainder of
45. This remainder can be calculated on the computer using MOD.
74 Numbers
Type
PRINT 12345 MOD 100
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
10 REM DICE1
20
30 FOR K=1 TO 10
40 LET X=RND(6)
50 PRINT X
60 NEXT K
and run the program. You should see ten numbers displayed on the
screen, just like the numbers you might get by throwing a dice
ten times. Line 40 generates a whole number at random, and stores
it in memory box X. The 6 in Line 40 tells the computer that the
number must be in the range 1-6 (that is, 1 or 2 or 3 or 4 or 5
or 6). To generate random numbers between 1 and 10 say, replace
RND(6) by RND(10).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Run the program again. The ten numbers now displayed will be
different from the previous numbers. In fact, each time you run
the program you will get a random set of numbers - you should not
be able to PREDICT what numbers will be displayed. In this sense,
the numbers are said to be random numbers.
Questions 75
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
When the program is working correctly, save it on your UNIT
PROGRAMS cassette as DICE1. Don't forget to fill in the 'cassette
contents' sheet.
PRINT 3+3*3
PRINT 7-4+2
PRINT 3*4/2
PRINT (1+3)*3-4/2
PRINT 9 DIV 4
PRINT 9 MOD 4
Try them on the computer, and check whether you were right.
The BBC computer can generate sounds through its internal loud-
speaker. There are two commands which control the sounds produced
- these are SOUND and ENVELOPE. We will only deal with the SOUND
command in this book. It has the form:
SOUND 1, A, P, D
P value : 53 61 69 73 81 89 97 101
Note : Middle C D E F G A B C
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
SOUND 1,-15, 53, 10
SOUND 1, -7, 53, 10
SOUND 1, -1, 53, 10
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
SOUND 1,-15, 53, 5
SOUND 1,-15, 53, 10
SOUND 1,-15, 53, 20
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
10 FOR K=1 TO 8
20 READ P
30 SOUND 1,-15, P, 10
40 NEXT K
50 DATA 53, 61, 69, 73, 81, 89, 97, 101
60 END
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
35 ..SOUND 1,0,0,3
replacing the two dots by two spaces. The musical scale will
again sound, but there will be a distinct pause between each
note, produced by Line 35 sounding a note of zero loudness and
zero pitch for a very short duration.
121
117
109
101
97
89
81
73
69
61
53
note: C D E F G A B C D E F
pitch: 53 61 69 73 81 89 97 101 109 117 121
An example should help to clarify all this. The music for the
Nursery Rhyme 'Jack and Jill' is:
121
117
109
101
97
89
81
73
69
61
53
The program shown opposite will play this tune. Each note of the
tune has a corresponding DATA statement specifying the pitch and
duration of that note.
The first note of the music is G. You can read off the pitch
value for this note at the end of the stave. It is 81. The
duration of the note is 1 time unit. This information is coded in
Line 100 of the program as DATA 81,1.
14.2 Playing music on your BBC computer 79
The following program will play the Nursery Rhyme 'Jack and Jill'
on the BBC computer.
10 REM MUSIC1
20
30 LET TIMEUNIT=10
40 REPEAT
50 READ PITCH,DURATION
60 SOUND 1, -15, PITCH, DURATION*TIMEUNIT
70 SOUND 1, 0, 0, 3
80 UNTIL DURATION=0
90
100 DATA 81,1
110 DATA 81,.5
120 DATA 81,1
130 DATA 81,.5
140 DATA 53,1
150 DATA 53,.5
160 DATA 53,1
170 DATA 53,.5
180
190 DATA 61,1
200 DATA 61,.5
210 DATA 61,1
220 DATA 61,.5
230 DATA 69,1.5
240 DATA 53,1.5
250 DATA 81,1
260 DATA 81,.5
270 DATA 81,1
280 DATA 81,.5
290
300 DATA 89,1
310 DATA 89,.5
320 DATA 89,1
330 DATA 89,.5
340 DATA 81,1
350 DATA 73,.5
360 DATA 69,1
370 DATA 61,.5
380 DATA 53,1.5
390 DATA 53,1.5
400
410 DATA 0,0
420 END
80 Sound
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter the program into the computer, and check that you have
typed it correctly. Run the program. Does it perform correctly?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
When the program is working correctly, save it on your UNIT
PROGRAMS cassette as MUSIC1. Don't forget to fill in the
'cassette contents' sheet.
Question
121
117
109
101
97
89
81
73
69
61
53
Your Electronic
program 12345 clock inside
TIME computer
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
PRINT TIME
The number displayed on the screen shows the amount of time (in
hundredths of a second) that has passed since the computer was
first switched on.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
LET T=TIME
The variable T now holds the value that was in TIME when this
command was executed. This is the amount of time that has passed
since the computer was first switched on.
Type
PRINT T
Type
PRINT T DIV 100, T MOD 100
a second.
Type
PRINT TIME DIV 100, TIME MOD 100
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
10 TIME=0
20 REPEAT
30 LET T=TIME
40 PRINT T DIV 100, T MOD 100
50 UNTIL FALSE
60 END
and run the program. The value of the TIME variable (in seconds
and hundredths of a second) is repeatedly displayed on the
screen. The UNTIL FALSE means the loop executes for ever (or
until you interrupt it). Let the program run for 10-20 seconds,
and then press the ESCAPE key. Study the program, and try to
understand how it works. Notice that you can give a value to the
TIME variable: we set it to zero in Line 10.
Now look carefully at the output displayed on the screen,
and notice that the second number (the hundredths) increases in
steps of 2 (and occasionally 3). Why does it not increase in
steps of 1, since the electronic clock adds 1 to TIME every one-
hundredth of a second? The answer is that our program takes more
than two hundredths of a second to execute Lines 40 and 50.
Hence, when the computer gets back to Line 30, and samples the
value in TIME again, it has increased by two or three hundredths
of a second.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you have a stop-watch, then try the following experiment to
see how accurate the clock within the computer is.
10 LET DELAY=200
20 TIME=0
30 LET NOW=TIME
40 REPEAT UNTIL TIME>=NOW+DELAY
50 PRINT TIME
60 END
Enter the program, and then run it. The program should pause for
two seconds before printing the current value in TIME. You will
find that the value is actually 201, the extra one-hundredth of a
second being used to execute the PRINT command itself.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
10 LET DELAY=500
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
10 FOR DELAY=1500 TO 7500 STEP 1500
20 TIME=0
30 FOR K=1 TO DELAY : NEXT K
40 PRINT DELAY; " "; TIME
50 NEXT DELAY
60 END
and run the program. You can use a FOR loop to get the computer
to pause for a period of time, as we have done in Line 30. Notice
in Line 30 that we have put the FOR and the NEXT on the same
line, separated by a colon; normally you should put them on
separate lines, but in this particular example it makes the
program clearer to have them on the same line. The length of the
pause can be varied by changing the final value in the FOR loop.
84 Timing
This program uses the TIME variable to measure the time that it
takes a person to react to some event.
10 REM REACT1
20
30 CLS
40 LET ADDUP=0
50
60 FOR K=1 TO 10
70 LET DELAY=200+RND(500)
80 LET NOW=TIME
90 REPEAT UNTIL TIME>NOW+DELAY
100 SOUND 1, -15, 53, 5
110
120 TIME=0
130 INPUT "" X$
140 LET T=TIME
150
160 LET ADDUP=ADDUP+T
170 PRINT T
180 NEXT K
190 PRINT
200 PRINT "AVERAGE = ";ADDUP/10;" HUNDREDTHS"
210 END
Type the program into the computer, and check that that it is
correct. Now run the program, and make a note of your average
reaction time. The following notes may help you to understand how
this program works.
Lines 120-140 measure how long it takes you to press the key.
This time is displayed by Line 170.
Questions 85
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Save the program on your UNIT PROGRAMS cassette as REACT1. Don't
forget to fill in the 'cassette contents' sheet.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
100 ..PRINT "PRESS";
replacing the two dots by two spaces. Instead of the bell being
sounded, the word PRESS is displayed on the screen. As soon as
you see the word on the screen, then press the RETURN key as
quickly as you can. Run the program, and make a note of your
average reaction time. You will probably find that this average
is larger than the previous average - indicating that you react
more quickly to signals from your ears than you do to signals
from your eyes.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Save the program on your UNIT PROGRAMS cassette as REACT2. Don't
forget to fill in the 'cassette contents' sheet.
Questions
Taking the first example, the condition is 'you are hungry' and
the action is 'eat some food'. If the condition is true, then you
take the action.
A computer can also take decisions. Indeed, it is this
capability which makes it into such a powerful and versatile
piece of equipment. A computer can take different actions
depending on the data that it is given. The command which tells
the computer to do this is the IF command.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
10 FOR AGE=8 TO 11
20 PRINT AGE;
30 PRINT
40 NEXT AGE
50 END
Did you notice the semi-colon at the end of Line 20? Now run the
program. The output should be:
8
9
10
11
16.1 The IF command 87
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The local football club runs a junior team called the under-10's.
Only boys who are under 10 years of age are eligible to play.
Let's get the computer to tell us the ages that can play for this
team.
10 FOR AGE=8 TO 11
20 PRINT AGE;
25 IF AGE<10 THEN PRINT " IS OK";
30 PRINT
40 NEXT AGE
50 END
8 IS OK
9 IS OK
10
11
When AGE has a value of 9, then the condition AGE<10 is also true
(9 is less than 10), and so the message 'IS OK' is printed.
When AGE has a value of 10, then the condition AGE<10 is false
(10 is not less than 10, it is actually equal to 10), and so the
message 'IS OK' is not printed.
When AGE has a value of 11, then the condition AGE<10 is again
false (11 is not less than 10, it is actually greater than 10),
and so the message 'IS OK' is not printed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The local school is organising a class outing. However, only
children who are over 8 years of age can go, because it would be
too tiring for younger children. Let's get the computer to tell
us the ages that can go on this outing.
88 The IF command
replacing the two dots by two spaces. Now run the program. The
output should be:
8
9 IS OK
10 IS OK
11 IS OK
There are FOUR other conditions that are commonly used in BASIC:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
A children's competition has been organised by the local library.
However, only 10-year olds can enter. Let's get the computer to
tell us the ages that can enter this competition.
and run the program. The '=' in Line 25 means is equal to. Is the
16.2 What conditions can be tested? 89
output what you would expect? The message 'IS OK' should only be
printed alongside an age of 10.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
For those children who were not eligible to enter the competition
the library has organised a film show.
Can you work out what ages (from 8, 9, 10, and 11-year olds) can
go to the film show?
and run the program. The '<>' in Line 25 means is not equal to.
If 10 is the only age that can enter the competition, then those
children who are NOT 10 (i.e. 8, 9, and 11-year olds) can go to
the film.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
A swimming club is entering a team for a swimming gala. One of
the age groups is '9 and under'. This means that only children
who are 9 or under 9 can be entered. Let's get the computer to
tell us the ages that can be entered in this age group.
and run the program. The '<=' in Line 25 means is less than or
equal to. Hence, Line 25 selects all children who are '9 or under
9'.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Only children who are '10 or over' are allowed to take the
YOURTOWN cycling proficiency test. Let's get the computer to tell
us the ages that can take this test.
and run the program. The '>=' in Line 25 means is greater than or
equal to. Hence, Line 25 selects all children who are '10 or over
10'.
90 The IF command
10 REM GUESS1
20
30 CLS
40 PRINT "THE COMPUTER WILL THINK OF"
50 PRINT "A NUMBER BETWEEN 1 AND 100"
60 PRINT
70 PRINT "TRY TO GUESS IT"
80 LET NUMBER=RND(100)
90
100 REPEAT
110 PRINT
120 INPUT "ENTER GUESS ",GUESS
130 IF GUESS>NUMBER THEN PRINT "TOO LARGE"
140 IF GUESS<NUMBER THEN PRINT "TOO SMALL"
150 UNTIL GUESS=NUMBER
160 SOUND 1, -15, 53, 5
170 PRINT "WELL DONE"
180 END
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
16.4 Modifying the pocket money program 91
Type in the program. List it, and then check that you have typed
it correctly. Now run the program. Does it work correctly? If
not, go back and re-check, correcting any mistakes.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
When the program is correct, save it on your cassette as GUESS1.
Don't forget to fill in the 'cassette contents' sheet.
You may remember that the pocket money program of Unit 7 prints
the pocket money when an AGE of 0 is entered to finish the
program. This is a bit untidy. Let us see how we can use the IF
command to overcome this problem.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Load POCKET2 from your UNIT PROGRAMS cassette. Run the program,
and make sure that it is working correctly.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Modify the program to become:
10 REM POCKET3
20
30 REPEAT
40 INPUT "ENTER AGE ",AGE
45 IF AGE=0 THEN 80
50 LET PAY=5*AGE
60 PRINT "POCKET MONEY = ";PAY
70 PRINT
80 UNTIL AGE=0
90 END
We have inserted line 45, which shows the IF command being used
differently. This version of the IF command has the form:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
When the program is correct, save it on your cassette as POCKET3.
Don't forget to fill in the 'cassette contents' sheet.
92 The IF command
is less than?
is greater than?
is equal to?
is not equal to?
under 10?
equal to 8?
9 or under?
over 10?
10 or over?
not 9?
Questions 93
condition AGE
AGE<7 5 6 7 8 9
AGE>8 5 6 7 8 9
AGE<>7 5 6 7 8 9
AGE>=7 5 6 7 8 9
AGE=8 5 6 7 8 9
AGE<=8 5 6 7 8 9
AGE>3 5 6 7 8 9
AGE<5 5 6 7 8 9
10 LET A=17
20 LET B=8
30 IF A+B>25 THEN 70
40 ...
If the HOME team wins the match, the result is a 'HOME win'.
If the AWAY team wins the match, the result is a 'AWAY win'.
If the scores are equal, the result is a 'DRAW'.
and then print the result of the match (HOME, AWAY or DRAW).
17.1 Introduction
LET DELAY=200+RND(500)
LET NOW=TIME
REPEAT UNTIL TIME>=NOW+DELAY
DEF PROC_PAUSE
LET DELAY=200+RND(500)
LET NOW=TIME
REPEAT UNTIL TIME>=NOW+DELAY
ENDPROC
Type
10 TIME=0
20 PROC_PAUSE Main program
30 PRINT TIME
40 END
50
60 DEF PROC_PAUSE
70 LET DELAY=200+RND(500)
80 LET NOW=TIME Procedure
90 REPEAT UNTIL TIME>=NOW+DELAY
100 ENDPROC
and list the program. Check that you have typed it correctly.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
TRACE ON
RUN
and check that the commands are executed in the order that you
expect. Line 60 (the DEF PROC... line) does not appear in the
trace - the first line of the procedure is actually Line 70.
Notice the pause in the trace at Line 90 while the computer
executes the REPEAT loop.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
96 Procedures
Type
35 PROC_PAUSE
36 PRINT TIME
and run the program again. Notice that the procedure is now
called twice. Each time, the computer begins at Line 70, and then
executes Line 80, 90, and 100. However, when the procedure is
finished, the computer returns to Line 30 after the first call,
and to Line 36 after the second call. Hence, the computer
remembers the line-number of the line calling the procedure, so
that it can return correctly.
17.3 Parameters
10 TIME=0
20 PROC_PAUSE(200)
30 PRINT TIME
40 END
50
60 DEF PROC_PAUSE(DELAY)
70 NOW=TIME
80 REPEAT UNTIL TIME>=NOW+DELAY
90 ENDPROC
and run the program. The procedure should cause a delay of about
2 seconds. Lines 60-90 define a procedure called PROC_PAUSE.
Notice that we have now written the name of a variable (DELAY) in
brackets immediately after the name of the procedure.
The procedure is called in Line 20, with a value of 200
written in brackets following the procedure name. This number of
200 is the parameter; it is the value we want the procedure to
work with on this occasion. Before executing the first command of
the procedure, the variable (DELAY) is set to 200. The computer
then executes the commands in the procedure, with DELAY having a
value of 200.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17.3 Parameters 97
20 PROC_PAUSE(500)
and run the program again. This time, the procedure is called
with a parameter of 500. Before executing the first command of
the procedure, the variable DELAY in the procedure is set to 500.
The computer then executes the commands in the procedure, with
DELAY having a value of 500. Hence, there should be a delay of
about 5 seconds.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
20 PROC_PAUSE(200 + RND(500))
and run the program again. Notice that the parameter may involve
arithmetic (we have used + and RND here).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type
15 LET WAIT=500
20 PROC_PAUSE(WAIT)
and run the program again. Notice that the parameter may be a
variable (we have used WAIT here).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
10 REM HOUSE4
20
30 MODE 1
40 LET BLACK=0
50 LET RED=1
60 LET YELLOW=2
70 LET WHITE=3
80 GOTO 500
90
100 DEF PROC_PAPER(colour)
110 GCOL 0,(128+colour)
120 CLG
130 ENDPROC
140
150 DEF PROC_INK(colour)
160 GCOL 0,colour
170 ENDPROC
180
190 DEF PROC_LINE(X1,Y1, X2,Y2)
200 MOVE X1,Y1
210 DRAW X2,Y2
220 ENDPROC
230
240 DEF PROC_RECT(X1,Y1, X2,Y2)
250 MOVE X1,Y1
260 MOVE X2,Y1
270 PLOT85,X1,Y2
280 PLOT85,X2,Y2
290 ENDPROC
300
310 DEF PROC_TRI(X1,Y1, X2,Y2, X3,Y3)
320 MOVE X1,Y1
330 MOVE X2,Y2
340 PLOT85,X3,Y3
350 ENDPROC
360
370 DEF PROC_CIRC(XC,YC, radius)
380 LOCAL X,Y,K,ANGLE
390 LET X=XC : LET Y=YC+radius
400 FOR K=5 TO 360 STEP 5
410 MOVE XC,YC
420 MOVE X,Y
430 LET ANGLE=K*PI/180
440 LET X=XC+radius*SIN(ANGLE)
450 LET Y=YC+radius*COS(ANGLE)
460 PLOT85,X,Y
470 NEXT K
480 ENDPROC
100 Procedures
490
500 PROC_PAPER(WHITE)
510
520 PROC_INK(RED)
530 PROC_RECT(200,300, 1000,600)
540 PROC_TRI(200,600, 400,600, 400,800)
550 PROC_RECT(400,600, 800,800)
560 PROC_TRI(800,600, 800,800, 1000,600)
570
580 PROC_INK(YELLOW)
590 PROC_RECT(500,300, 600,500)
600
610 PROC_INK(YELLOW)
620 PROC_RECT(300,400, 400,500)
630 PROC_RECT(700,400, 900,500)
640
650 PROC_INK(BLACK)
660 PROC_TRI(200,0, 500,0, 500,300)
670 PROC_TRI(600,300, 500,0, 500,300)
680
690 PROC_INK(WHITE)
700 PROC_LINE(200,600, 1000,600)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Type the HOUSE4 program into your computer. List it and check
that you have typed it correctly. Now run the program. Is the
picture drawn on the screen the same as Figure 9.1? The wall and
roof should be red, the door and windows yellow, the path black,
and the background white.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
When the program is working correctly, save it on your UNIT
PROGRAMS cassette as HOUSE4. Don't forget to fill in the
'cassette contents' sheet.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
You can now experiment with this program. For instance, you could
draw the sun in the top right-hand corner, using PROC_CIRC.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Lines 10-510 of this program form the basis of other picture-
drawing programs; you simply change lines from 520 onwards, using
any combination of the procedure calls to draw the picture you
want.
Delete Lines 520-700. Change the title in Line 10 to PROCS1.
Now save the program on your UNIT PROGRAMS cassette as PROCS1.
Don't forget to fill in the 'cassette contents' sheet.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Questions 101
100 200 300 400 500 600 700 800 900 1000 1100 1200
1000
1000
900 900
800 800
700 700
y co-ordinate
600 600
500 500
400 400
300 300
200 200
100 100
0
100 200 300 400 500 600 700 800 900 1000 1100 1200
x co-ordinate
Unit 3: Question 9
10 LET SPEED=60
20 PRINT SPEED
30 LET HOURS=4
40 PRINT HOURS
50 LET MILES=SPEED*HOURS
60 PRINT MILES
70 END
Unit 7: Question 5
10 REM SHOP1
20
30 LET BILL=0
40 REPEAT
50 INPUT "ENTER ITEM ",ITEM
60 LET BILL=BILL+ITEM
70 UNTIL ITEM=0
80 PRINT
90 PRINT "TOTAL BILL = ";BILL
100 END
Unit 9: Question 9
140
150 MOVE 500,300
160 DRAW 500,500
170 DRAW 600,500
180 DRAW 600,300
190 DRAW 500,300
Answers 103
200
210 MOVE 300,400
220 DRAW 300,500
230 DRAW 400,500
240 DRAW 400,400
250 DRAW 300,400
260
270 MOVE 700,400
280 DRAW 700,500
290 DRAW 900,500
300 DRAW 900,400
310 DRAW 700,400
320
330 MOVE 500,300
340 DRAW 200,0
350
360 MOVE 600,300
370 DRAW 500,0
380 END
35 GCOL 0,131
36 CLG
37 GCOL 0,1
145 GCOL 0,2
325 GCOL 0,0
10 REM MONTHS2
20
30 INPUT "MONTH NUMBER = ",M
40 FOR K=1 TO M
50 READ MONTH$,DAYS
60 NEXT K
70 PRINT "MONTH ";M;" = ";MONTH$
75 PRINT "IT HAS ";DAYS;" DAYS"
80
90 DATA "JAN", 31
100 DATA "FEB", 28
110 DATA "MAR", 31
... ....
200 DATA "DEC", 31
210 END
104 Answers
10 REM COIN1
20
30 FOR K=1 TO 20
40 LET X=RND(2)
50 PRINT X
60 NEXT K
70 END
10 REM FTBALL1
20
30 REPEAT
40 INPUT "HOME TEAM GOALS = ",HOME
50 IF HOME<0 THEN STOP
60 INPUT "VISITORS GOALS = ",VISITORS
70
80 IF HOME>VISITORS THEN PRINT "HOME WIN"
90 IF HOME<VISITORS THEN PRINT "AWAY WIN"
100 IF HOME=VISITORS THEN PRINT "DRAW"
110 PRINT
120 UNTIL FALSE
130 END
Notice the STOP command in Line 50. It has the same effect
as the END command in that it stops the execution of the
program. In addition, however, the STOP command displays a
message on the screen showing the line-number at which the
program stopped.
Appendix B Summary of BASIC
CLS clear the text screen. The text area of the screen is
cleared, and left in the current text background
colour.
DATA stores numeric and string data items within the program
itself, for use by the READ command.
END tells the computer that it has reached the end of the
program.
LET The LET command puts a value into a memory box, for
example LET AGE=9.
STEP is the part of the FOR command which specifies the size
of the step to be added to the loop-control-variable
each time the loop is executed.
2D Write down the number that will be contained in each memory box
after the LET commands of Question 2C have been executed.
A B C D
2E Work out what values will be in the memory boxes after the
computer has processed each of the following commands. Write your
answers in the boxes provided.
Additional Questions 109
10 LET GOALS=30
20 PRINT GOALS
30 LET GAMES=10
40 PRINT GAMES
50 PRINT GOALS/GAMES
60 END
3B The following program works out the cost of 10 apples, when each
apple costs 5 pence. Complete the program by filling in the gaps.
3C The following program works out how many centimetres there are in
2 metres. Unfortunately, the commands have got mixed up. Put the
commands into their correct order.
PRINT CENTIMETRES
LET CENTIMETRES=METRES*100
PRINT METRES
END
LET METRES=2
10 LET 5=AGE
20 PRINT AGE
30 PRINT PAY=10 x AGE
30 PRINT PAT
50 END
4C The following program asks for two numbers to be entered via the
keyboard, then it displays their sum. Fill in the gaps.
10 INPUT .....
20 ..... SECOND
30 LET SUM = ..... ..... SECOND
40 PRINT .....
50 END
10 LET SPEED = 50
20 LET HOURS = 2
30 PRINT "SPEED OF CAR = "; SPEED; " MILES PER HOUR"
40 PRINT "TRAVELLING TIME = "; HOURS; " HOURS"
50 PRINT "DISTANCE TRAVELLED = "; SPEED*HOURS; " MILES"
60 END
10 REPEAT
20 PRINT "STILL IN THE LOOP"
30 PRINT "ENTER 0 TO EXIT"
40 INPUT "ENTER A NUMBER ",NUMBER
50 UNTIL NUMBER=0
60 END
Additional Questions 111
10 LET X=1
20 REPEAT
30 INPUT "ENTER NUMBER ",Y
40 UNTIL X=0
50 END
10 CLS
20 FIRST$="JOHN"
30 .....="SMITH"
40 PRINT ..... ; .....; LAST$
8F Write a program that displays your name and address on the screen
112 Additional Questions
11A Write a FOR command which will cause a loop to be executed with K
values of:
(a) 1 2 3 4 5
(b) 3 4 5 6
(c) 10 20 30 40 50
(d) 7 6 5 4
(e) the odd numbers from 1 to 9
(f) the even numbers from 12 to 18
(g) the numbers starting at 10 and counting in 5's until 40
11B The following FOR loops are all incorrect. Can you find the
mistake in each?
11D Write a program which uses a FOR loop to produce the following
output:
1 x 1 = 1
2 x 2 = 4
3 x 3 = 9
4 x 4 = 16
12A What values will the variables contain after the following
commands have been executed?
12D Make a list of names and ages of FOUR people, in the form:
DATA "MARTIN", 10
....
13D Fill in the boxes to show the values of the variables AFTER each
line of the program has been executed.
A B C D
10 READ A,B,C
20 LET D=A+B*C
30 LET B=(A+C)/B
40 DATA 4,3,2
50 END
10 READ NUMBER
20 RANDOM=RND(NUMBER)
30 FOR K=1 TO RANDOM
40 READ NAME$
50 NEXT K
60 PRINT "I HAVE CHOSEN ";NAME$
70 DATA 7
80 DATA "JOHN","ALAN","SUSAN","CHRISTINE"
90 DATA "ANDREW","JONATHAN","ALISON"
100 END
16B Using only < and >, complete the following statements:
16C Using only <= and >=, complete the following statements:
10 FOR K=1 TO 4
20 READ X
30 PRINT X;
40 IF X<10 THEN PRINT " is less than 10"
50 IF X=10 THEN PRINT " is equal to 10"
60 IF X>10 THEN PRINT " is greater than 10"
70 NEXT K
80 DATA 8,9,10,11
90 END
Additional Questions 115
17A Using the procedures described on Page 98, write commands to:
2B (a) AGE (b) A and B (c) HEIGHT (d) none (e) X (f) PRICE
2C (a) LET A=5 (b) LET B=4+3 (c) LET C=A-2 (d) LET C=C+1
(e) LET D=A+C
3A 30 10 3 on separate lines
3C LET METRES=2
PRINT METRES
LET CENTIMETRES=METRES*100
PRINT CENTIMETRES
END
3E 10 LET KILOGRAMS=3
20 PRINT KILOGRAMS
30 LET GRAMS=KILOGRAMS*1000
40 PRINT GRAMS
50 END
7B START NOW
ENTER NUMBER ?1
2
ENTER NUMBER ?2
4
ENTER NUMBER ?3
6
7C 10 20 30 40 50 30 40 50 30 40 50 60
7D It will keep printing ENTER NUMBER until you press the ESCAPE key
or the BREAK key.
7E 10 LET ADDUP=0
20 REPEAT
30 INPUT "ENTER AGE ",AGE
40 ADDUP=ADDUP+AGE
50 UNTIL AGE=0
60 PRINT "TOTAL AGE = ";ADDUP
70 END
8D INPUT NAME$
11C 6
12C DRILL
SCREWDRIVER
HAMMER
13D A B C D
READ A,B,C 4 3 2
LET D=A+B*C 10
LET B=(A+C)/B 2
13E (a) LET X=RND(5) (b) LET X=5+RND(5) (c) LET X=10*RND(5)
16B (a) < (b) > (c) > (d) < (e) < or >
16C (a) <= (b) >= (c) >= (d) <= (e) <= or >=
(b) PROC_INK(RED)
(d) PROC_INK(YELLOW)
(f) PROC_INK(BLACK)
Tables program 64
Tape 27
Television 2
Text 49
THEN 86, 107
TIME 81, 107
Timing 81-5
Title 28
TO 59-61, 107
TRACE 15, 18, 40, 95, 107
Triangle 54, 98
Units 1
Underline 12, 94
UNTIL 39, 43, 107
Variable names 12
All the important features of BASIC are covered in this
short introduction and the reader is shown how to use
these in simple programs. The book is divided into short
units. Each unit focuses on a particular feature of
programming on the BBC microcomputer and consists of
explanatory text, test questions and practical exercises. The
practical exercises can be undertaken individually or
demonstrated by a teacher on a single computer in a
classroom.
BBC BASIC
R. B. Coats
Spectrum BASIC
R. B. Coats
Applesoft BASIC
B. M. Peake
Microcomputing with the PET
J. Arotsky, J. Taylor and D. W. Glassbrook
Microcomputing in BASIC on the RML 380Z/480Z
W. R. McDonough
Computer Keyboard Mastery
Stan Harcourt
Basic BASIC
Donald M. Monro
Basic BASIC on the BBC MICRO (in preparation)
Donald M. Monro
Edward Arnold
ISBN 0 7131 3520 4