BASIC introduction Index Next chapter

Chapter 1

Starting with BASIC

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.

1.1 Entering BASIC

To use BASIC, load CP/M as described in Part I (section 1.1) and then:

Type BASIC [Enter]

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 [Enter] if you are using a 24 x 32 screen, or
Type WIDTH 80,80 [Enter] if you are using a 24 x 80 screen.

1.2 Leaving BASIC

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 [Enter]
A>

Before you can use BASIC again, you must enter it as described above.

1.3 Using BASIC

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.

1.4 Giving instructions to BASIC

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 [Enter].

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:

Type PRINT 42 [Enter]
\-v-/ \/
commandinformation

Note that BASIC does not try to obey the instruction until you complete it by pressing [Enter]; until then, you are free to alter what you have typed, by deleting characters with the [Delete] 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:

Type DIR [Enter]

1.5 Types of information

The information part of an instruction can take a wide variety of forms, as follows.

1.5.1 Fixed information (constants)

The simplest type of information to understand is that already used in the PRINT example above.

PRINT 42 [Enter]
\-v-/ \/
commandconstant(number)

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 42, 0.0000002, -11111111111) or 'text strings' (such as "Hello", "Returns for tax year 87/88", "42"):

PRINT "Hello, gentle reader" [Enter]
\-v-/ \----------v---------/
commandconstant(text string)

Note that text strings must be surrounded by the double quote character (").

1.5.2 Varying information (variables)

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:

numeric:a, count, count
string:a$, name$, address$

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:

Type vat=0.15 [Enter]
\-/ \--/
variableassigned value

You could then use this variable when you wanted the value 0.15. For example:

Type PRINT vat [Enter]
\-v-/ \/
commandvariable

Similarly by defining a string variable, such as 'name$':

Type name$="Anne Elizabeth Barnard" [Enter]

You can print this name easily and quickly, using:
Type PRINT name$ [Enter]
\-v-/ \-v-/
commandvariable

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.

Calculated information (expressions)

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:

Type PRINT 42+1 [Enter]
and Type PRINT 43-1 [Enter]

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 following:

Type PRINT 42*2 [Enter]
and Type PRINT 42/3 [Enter]

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 [Enter] the third time, try and predict what will happen.

Type first$="Annie " [Enter]
Type last$="Oakley " [Enter]
Type PRINT first$ + last$ [Enter]

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:

Type PRINT 22+44 [Enter]
66
Ok
Type PRINT 66+90000032 [Enter]
90000098
Ok
Type PRINT 90000098-400311 [Enter]

Or, to save a lot of typing, you can use a single, more complex, expression to get an answer with a single instruction:

Type PRINT 22+44+9000032-400311 [Enter]

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. For instance, 3+4*5 could be calculated as:

3 + 4 = 7
* 5 = 35

or

4 * 5 = 20
+ 3 = 23

The BASIC operators and functions have priorities (called the 'operator precedence') which determine the order in which they are applied. For example, * and / are applied to the information to each side of them before + and -, so 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 (7)*5 while 3+(4*5) is evaluated as 3+(20).

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:

Type PRINT SQR(121) [Enter]
11
Ok

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) [Enter]
Ann
Ok

(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:

Type PRINT LEN(name$) [Enter]
22
Ok

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).

1.6 Chapter review

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