|BASIC introduction||Index||Next chapter|
This chapter tells you how to load BASIC and use it as a simple calculator. It also introduces many concepts that are helpful to know before you start programming in BASIC.
To use BASIC, load CP/M as described in Part I (section 1.1) and then:
After a few seconds, the screen should display a message confirming that BASIC has been selected, similar to the following:
Mallard-80 BASIC with Jetsam Version n.nn © Copyright 1984 Locomotive Software Ltd All rights reserved nnnnn free bytes Ok
Once this message is displayed, you have access to all the facilities of BASIC. If this message is not displayed, check your typing and that you have inserted the right disc the right way round.
Note: BASIC on the Spectrum expects you to be using the 24 x 51 size of screen. If you are using a different screen size, you should now:
|Type||WIDTH 32,32||if you are using a 24 x 32 screen, or|
|Type||WIDTH 80,80||if you are using a 24 x 80 screen.|
If you want to use a different language or application, you must leave BASIC and return to the computer's operating system. You can either re-boot your computer or use the BASIC SYSTEM command, as follows:
|Type|| SYSTEM |
Before you can use BASIC again, you must enter it as described above.
Although BASIC is a programming language, it can also be used as a very powerful calculator. Using BASIC as a calculator provides a gentle introduction to many of the fundamental concepts you will need to understand before you can begin to program.
The rest of this chapter introduces you to using BASIC in this way. Using BASIC for programming is described in the next chapter.
To get BASIC to do anything, you must tell it what to do, by typing instructions for it to obey. An instruction starts with a word that has a special meaning for BASIC, called a 'command', which may be followed by information used by that command, and is finished by pressing .
Commands are one of the group of words and symbols that have a special meaning to BASIC called the 'BASIC keywords'. Although you can type them using capitals or small letters, they will always be shown as capitals in this book to help you pick them out (they are also always displayed in capitals whenever you examine your programs).
Commands are rather like the verbs in an English sentence; they indicate an action. There are a wide variety of commands, which will be introduced over the next few chapters. One of the simplest to understand is PRINT. This command tells BASIC to display the information contained in the rest of the instruction - to 'print' it on the screen.
The information part of a command is rather like the object in an English sentence: it tells the computer what to apply the command to. In the case of PRINT, it tells the computer what to display.
Thus, to get BASIC to print the number 42 on the screen:
Note that BASIC does not try to obey the instruction until you complete it by pressing ; until then, you are free to alter what you have typed, by deleting characters with the key and retyping.
You should note from the outset that like most other computer facilities, BASIC will only recognise commands if you spell them correctly. Misspelt commands will normally produce a message from BASIC such as Syntax Error. If you get an unexpected message of this sort, for now just check your typing and try again. In particular, make sure that you leave a space between the command and any information following it.
Another useful and simple command is DIR, which behaves almost exactly as the operating system command of the same name. For instance, to list the names of all files on the currently selected disc:
The information part of an instruction can take a wide variety of forms, as follows.
The simplest type of information to understand is that already used in the PRINT example above.
In this, a number has been specified by typing it in the instruction. This type of information is
called a 'constant', because its value is fixed - obvious from the instruction. Constants can
either be numbers (such as
-11111111111) or 'text
strings' (such as
"Returns for tax year 87/88",
|"Hello, gentle reader"|
Note that text strings must be surrounded by the double quote character (
A second type of information is called a 'variable'. A variable is a name for an item of information rather than the information itself; when BASIC obeys an instruction containing a variable, it substitutes the value that it has stored for that variable (given by a previous instruction) for the variable name.
But why should you use a variable rather than put the constant value you want in the instruction? By using a variable, you make the instruction versatile - by changing the value of the variable, you can use the same instruction over and over, rather than have a different instruction for each value.
There are different types of variable to store different types of information. The two main types are 'numeric' (for numbers) and 'string' (for text strings). Numeric variables have names consisting of any of the letters 'A' to 'Z', 'a' to 'z', '0' to '9' and '.'; string variables have similar names but must end with the character '$'. The first character of a variable name must be a letter, not a number.
Note that although you can use small or capital letters in variable names, they are considered identical by BASIC. For example, 'dog', 'Dog' and 'DOG' are all the same variable, even though they look different. In this user guide, variable names will be shown using small letters to help you distinguish them from keywords.
Typical variable names are as follows:
You are free to choose variable names with only a few limitations, the most significant of which is that the variable name must not be the same as a BASIC keyword (as listed in Appendix IV) and may not contain embedded spaces.
The main way to give a value to a variable is to 'assign' the value to it, using the
equals sign (
=). For example, to give the variable 'vat' a value of 0.15, you could:
You could then use this variable when you wanted the value 0.15. For example:
Similarly by defining a string variable, such as 'name$':
|Type||name$="Anne Elizabeth Barnard"|
You can print this name easily and quickly, using:
The value of a variable is changed, but not fixed by assigning to it: its value can be changed as many times as you want. When the variable is used in an instruction, the value most recently assigned to it will be used.
It may surprise you to see the form that that the assignment instruction takes particularly if you have used Spectrum BASIC. Where is the LET command telling BASIC what to do?
The answer is that Mallard BASIC analyses the instruction, sees the equals sign and interprets it as if there were an invisible assignment command at the start of the instruction! You can use LET if you wish, but it is not necessary.
The third type of information which can be used in an instruction is an 'expression'. An expression is an instruction to BASIC to calculate the information intended. It consists of one or more constants and/or variables, combined with BASIC 'operators' or 'functions. The operators and function instruct BASIC how to manipulate the information provided with them to calculate the required information.
Operators Operators are in general single symbols that are used between two items of information, providing a result which is derived from them both.
The simplest expression to understand is one using the familiar operators for addition and subtraction. Try the following instructions using expressions with these operators:
Their meanings and effects should be obvious.
Two other highly useful operators are for multiplication (
*) and division
/). While the symbols used may be unfamiliar, their actions are not. Try the
These operators work with numeric constants and variables. The only one that works with
strings is '
+', which adds one string to the end of another. Type the following
exactly as shown, but before pressing
the third time, try and predict what will happen.
|Type||PRINT first$ + last$|
Were you right?
Expressions need not be this simple. For example, to add 22, 44 and 90000032, then take 400311 from the result, you could use three separate instructions:
Or, to save a lot of typing, you can use a single, more complex, expression to get an answer with a single instruction:
Although there is a limit on the length and complexity of the expressions you can use, you are unlikely to exceed it.
Once an expression contains more than one operator it is not always obvious how it will be
evaluated. This is because the order in which the operators are applied can alter the result.
3+4*5 could be calculated as:
The BASIC operators and functions have priorities (called the 'operator precedence') which
determine the order in which they are applied. For example,
are applied to the information to each side of them before
3+4*5 is evaluated as
3+(4*5) = 23. To be certain that the calculation
is carried out correctly, put round brackers around any part of an expression that you want BASIC
to evaluate as a self-contained whole, using the result in the expression. For example,
(3+4)*5 is evaluated as
3+(4*5) is evaluated
Functions Functions have names (rather like BASIC commands) and are followed by the information they manipulate, in brackets. A typical function is SQR, which provides a square root:
A typical function that works with strings is LEFT$, which extracts a specified number of
characters from the start of a string. For example, to print the first three letters from
name$ (set up on page 132):
|Type||PRINT LEFT$(name$, 3)|
(Note how the LEFT$ function uses two items of information, separated by a comma.)
A third type of function uses information of one type but provides another. For example,
to tell how long
name$ is, instead of counting the number of characters yourself,
let BASIC do it for you:
BASIC provides a wide variety of functions such as cosines, logarithms, extracting text from the middle of strings, converting strings to capitals and converting strings to numbers and vice versa. These are described in further detail in Chapter 5. You can even add your own functions to BASIC (using DEF FN, as described in Chapter 5).
In this chapter, you learned how to enter and leave BASIC, and about BASIC instructions, commands and information. You now know how to make BASIC perform simple tasks like calculating and displaying simple sums.
|BASIC introduction||Index||Next chapter|