Previous chapter Index Next chapter

Chapter 2

Starting programming with BASIC

This chapter introduces you to fundamental concepts of programming and the simpler tools BASIC provides to help you write and use programs.

2.1 A first program

So far, although you have learned how to use a very limited number of BASIC facilities, you have not written or used a program. This can soon be corrected. Type the following exactly as shown:

    NEW  [Enter]
    10 PRINT 42  [Enter]

You have now written your first program!

The first line, the command NEW, instructed BASIC to forget any program it might have stored, ready for you to construct a new program from scratch.

The second line is your program. It consists of an instruction (the very first one you met, above), preceded by a number, known as a 'line number'. The line number does two main things: it tells BASIC to store the instruction rather than obey it directly, and gives the stored instruction a label, which can be used for a variety of purposes.
10 PRINT 42 [Enter]
\/ \--v---/
line number instruction

That is all a program is: one or more BASIC instructions typed on separate lines, each of which is preceded by a (different) number.

So how is this simple program different from the similar instruction? Programs have two extra properties:

To make BASIC obey the program you have typed, you use the command RUN:

Type RUN [Enter]
42
Ok

(If you get some other message, there is an error in your program. Start again with NEW etc. and be more careful with your typing!)

To store your program on disc, use the command SAVE. Programs are stored by your computer's operating system as named files; you specify the name to use by giving a string after SAVE:

Type SAVE "NUMPROG" [Enter]
Ok

The file name should only include a '.' if you want to specify a file type.

To use a program that is stored on disc, you can make BASIC read it from the disc using the command LOAD, then run it:

Type LOAD "NUMPROG" [Enter]
Ok

Type RUN [Enter]
42
Ok

Alternatively, you can load and run it with one instruction, by using the command RUN with the program name:

Type RUN "NUMPROG" [Enter]
42
Ok

Note: The program that you load doesn't have to have been typed in within BASIC. It can equally well have been prepared using a word processor or some other text editor and then stored as an ASCII file (ie. as a simple text file).

Once the program has been read from the disc, you can use it over and over again by using RUN without a program name. You do not have to read it again each time. (If you are a real sceptic you can prove that this version of RUN really works, by turning your Spectrum off then on again, restarting BASIC and only then using the RUN command. This level of scepticism could prove rather inconvenient, though!)

If you use a DIR command to examine the disc, you will notice that BASIC has stored the program as a file with the name specified with the SAVE command plus the filetype '.BAS'. This filetype identifies the file as a BASIC program. You do not need to include this filetype when creating or using a program with SAVE, LOAD, RUN (or CHAIN), but you can include a filetype if you want to specify one other than '.BAS'.

2.2 More complex programs

If a program consists of only one line (like the example above), the line number is almost irrelevant. But as soon as you add more lines, the line numbers assume great importance: if the line number of a new line is the same as an existing line, the new line entirely replaces the old one; if the line number is different, the new line is added so that the program consists of lines in order of ascending line number.

For example, type the following four lines exactly as shown:

    NEW [Enter]
    30 PRINT 64*64 [Enter]
	10 PRINT "64 times 64" [Enter]
	20 PRINT "is:" [Enter]

To see how the program is stored by the computer, use the command LIST:

Type LIST [Enter]

This displays:

    10 PRINT "64 times 64"
	20 PRINT "is:"
	30 PRINT 64*64
    Ok

As you can see, despite the order in which you typed the lines, BASIC has stored them in the order of their line numbers. This is not just for show: it indicates the order in which the lines will be obeyed when the program is run. To check, run the program and see the results:

Type [Enter]
64 times 64
is
4096
Ok

BASIC even provides a command to supply line numbers for you automatically when typing in a new program. This command is AUTO:

AUTO [Enter]
generates line numbers starting at 10, increasing by 10 each time you press [Enter]
AUTO 620 [Enter]
generates line numbers starting at the specified start (620), increasing by 10 each time you press [Enter]
AUTO 100,20[Enter]
generates line numbers starting at the first number (100), increasing by the second number (20) each time you press [Enter]

To turn off AUTO once it has started, type the special character combination called Control-C. Control-C (and other similar Control-letter combinations) is typed by holding down the [Extend Mode] key and pressing the given letter key.

In the examples given in this guide, program lines usually contain only a single instruction. Program lines can however have more than one instruction on them, as long as they are all separated by colons. Thus, the preceding example program could be written all on one program line as:
10 PRINT "64 times 64": PRINT"is:": PRINT 64*64
\/ \----------v---------/ \--v---/ \--v---/
line number first instruction second instruction third instruction

There are occasions when this ability is very useful, but for now you are advised to stick to a single instruction per line, because this makes it easier to change programs, see how they work and trace the cause of the problem when they don't!

2.3 Changing a program

If you are not happy with a program, one way to 'change' it is to wipe it out using NEW and start all over again. While this is perfectly sensible for very short programs, it is a very inefficient way of making changes to large programs.

BASIC provides a number of commands to make developing programs easier than this:

If the program is too long to fit onto the screen in one go, you can use LIST and press Control-S to 'freeze' the display when the part that you want to see is on the screen, then press Control-S again to unfreeze the display and continue listing or Control-C to unfreeze the display but stop the listing.

Alternatively, if you know the line numbers of a part of the program you want to display, you can specify them with LIST, as a single number or range, as follows:

LIST 120 [Enter]
lists a single line of the program
LIST -80 [Enter]
lists from the start of the program up to and including the specified line
LIST 400- [Enter]
lists from the specified line to the end of the program
LIST 420-440 [Enter]
lists the lines in the specified range

Alternatively, you can list the program on the printer, using LLIST. This behaves exactly as LIST, except that the lines selected are printed on the printer, not the screen.

For example, to delete line 400:

Type 400[Enter]

To delete a range of lines, use the command DELETE with a line or range of lines specified as for LIST above.

Type RENUM[Enter]

This command changes the number of the first line in the program to 10, the next to 20, the next to 30, etc. References to these line numbers within the program are also adjusted accordingly. (RENUM is actually more versatile than this for example, RENUM 100 will change the first line number to 100, the second to 110 and so on - but the details are outside the scope of this introduction. See 'Mallard BASIC: Introduction and Reference' for details.)

For example, to edit line 100:

Type EDIT 100[Enter]

The EDIT command displays the current contents of the specified line and provides the following facilities for changing that line:

These actions can be carried out by using BASIC's Screen-based editor. This changes the line 'before your eyes' in response to your pressing particular keys on the keyboard.

The keystrokes to use on the Spectrum are as follows:

To demonstrate how you would use the Screen-based editor, suppose you had typed 20 PRNT instead of 20 PRINT. To edit this line, type:

    EDIT 20 [Enter]
    20 PRNT
Move the cursor to the N by pressing [>] twice
    20 PRNT
Then type I, giving you:
    20 PRINT

Finally press [Enter]

Note: You can also use the facilities of EDIT on the line you have just typed (whether a command or a program line) by typing [Extend Mode] A.

2.4 Chapter review

In this chapter you have learned how to create programs from the commands and using the types of information introduced in the previous chapter. You have also seen how to store programs on a disc and re-use them, and how to examine and change a program.


Previous chapter Index Next chapter