|Previous chapter||Index||Next chapter|
This chapter introduces you to fundamental concepts of programming and the simpler tools BASIC provides to help you write and use programs.
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 10 PRINT 42
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.
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:
(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:
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:
Alternatively, you can load and run it with one instruction, by using the command RUN with the program name:
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'.
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 30 PRINT 64*64 10 PRINT "64 times 64" 20 PRINT "is:"
To see how the program is stored by the computer, use the command LIST:
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:
|64 times 64|
BASIC even provides a command to supply line numbers for you automatically when typing in a new program. This command is AUTO:
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 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:
|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!
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:
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:
To delete a range of lines, use the command DELETE with a line or range of lines specified as for LIST above.
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
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 20 PRNTMove the cursor to the N by pressing twice
20 PRNTThen type I, giving you:
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 A.
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|