Chapter 7. Editing programs

The Electron provides you with a number of very useful facilities for laying out, editing and listing your programs. If you haven't done any programming before, here is a brief list of the sort of facilities you will need when typing in programs and making them work:

  • Being able to display part or the whole of your program on the screen whenever you want to
  • Correcting mistakes, or editing
  • Putting comments or notes into the program to help you remember what each part of the program is doing
  • Deleting one or more program lines

To start looking at these facilities and how to use them, type in the sample program below which we will use to demonstrate the different facilities.

First, press BREAK to clear the screen and reset the computer, then type the following and take care with the punctuation and spaces in the last line.

10 PRINT "GIVE ME A NUMBER BETWEEN ONE AND TEN"
20 INPUT X
30 Y=2*X
40 PRINT "TWO TIMES 2;X;" IS ";Y

After typing in the above program, type

RUN  RETURN

When you run this program, the following happens

line 10 GIVE ME A NUMBER BETWEEN ONE AND TEN appears on the screen
line 20 A question mark appears on the line below, and the computer waits for you to type in a number which is stored as a variable called X. Type in a number and press RETURN
line 30 The computer multiplies X by 2 and stores the result as a variable called Y
line 40 The following is printed on the screen: TWO TIMES (the number you typed in) IS (the result)

If the program won't work properly, or you get an error message, press SHIFT and type it again - you most likely made a mistake when you typed it in the first time.

Listing the Program

When you want to change your program in any way, you will need to display the program (or at least the bit you want) on the screen. To do this, use the BASIC command LIST. Type

LIST  RETURN

Your program appears immediately underneath the LIST command on the screen.

If you only want to look at one particular line, say line 40, type

LIST 40  RETURN

Line 40 of your program is displayed on the screen.

To look at a number of consecutive lines, say lines 20 to 40, type

LIST 20,40  RETURN

Lines 20, 30 and 40 appear on the screen.

If you want to see from the beginning of the program up to a particular line, say line 30, type

LIST ,30  RETURN

Lines 10, 20 and 30 appear on the screen.

If you want to see from a particular line to the end of the program, then type

LIST 20,  RETURN

Lines 20, 30 and 40 appear on the screen.

Please refer to chapter 25 for a description of the LISTO commands. These commands provide you with even more facilities when listing programs.

Editing Programs

There are three ways of correcting mistakes in programs you have typed.

One of these you have already met in chapter 5: that is, pressing the DELETE key which moves the cursor back along the current line deleting each character as it goes. There is one major drawback to this method - if you have finished typing a line and have pressed RETURN, you can't get the cursor to go back to that line by just pressing the DELETE key. As we said before, pressing the DELETE key only moves the cursor back along the current line, which may not be the one you want to correct.

Another method is to type in the line again, but with the correction. The computer always replaces the old program line with any new version you type in. If the line to be corrected is very short, then this method is fine; but if the line is long or complicated, then use the third method described below.

Editing with the arrow keys and the COPY key

Type:

LIST  RETURN

The program appears on the screen, and we are going to use it to try out some editing. The following should now be on your screen:

>LIST
   10 PRINT "GIVE ME A NUMBER BETWEEN ONE AND TEN"
   20 INPUT X
   30 Y=2*X
   40 PRINT "TWO TIMES ";X;" IS ";Y

Supposing you want to change the word BETWEEN to FROM in line 10.

First of all, press the up-arrow key five times. The original cursor position under line 40 becomes a white square, and the cursor moves up to line 10.

Now press the key marked COPY three or four times. The cursor moves along line 10, the white square moves along as well - and line 10 is copied underneath line 40. Keep on pressing the COPY key until you have copied the word BETWEEN, then stop. Note that if you hold the key down, the repeat action allows you to move the cursor quickly across the screen. A quick press and release gives you precise control, moving just one character position. The following should be on your screen:

>LIST
   10 PRINT "GIVE ME A NUMBER BETWEEN_ONE AND TEN"
   20 INPUT X
   30 Y=2*X
   40 PRINT "TWO TIMES ";X;" IS ";Y
>  10 PRINT "GIVE ME A NUMBER BETWEENâ– 

Now press DELETE until BETWEEN has been deleted from the new line 10.

Note that the cursor on the old line 10 hasn't moved.

If the cursor isn't in the right place, i.e. underneath the space separating BETWEEN and ONE, move it there now by using the arrow keys.

Now type in the word FROM, then press the COPY key to copy the rest of line 10 to your new version.

Press RETURN. The white square disappears and the cursor goes to the start of a new line. The result should be this:

>LIST
   10 PRINT "GIVE ME A NUMBER BETWEEN ONE AND TEN"
   20 INPUT X
   30 Y=2*X
   40 PRINT "TWO TIMES ";X;" IS ";Y
>  10 PRINT "GIVE ME A NUMBER FROM ONE TO TEN"
>_

If you LIST the program again, by typing

LIST  RETURN

you will see that the new line 10 has replaced the old version. Have a go at editing line 10 again and change AND to TO.

There are no restrictions on how much you move the cursor around when you're copying. This means that you can copy bits from lots of different lines onto your new line all in one go: wherever you move the cursor to by using the arrow keys, you can then copy as much as you like of that line, then move the cursor somewhere else and continue copying.

Deleting Lines from your Program

There are two ways of deleting whole lines from your program.

The first method is to type in the line number of the line you want to delete, then press RETURN. What you are doing is entering a new version of that line into the computer - only with nothing in it. Because the computer always replaces a previous version of a line with any new version you type in, this method effectively deletes that line number.

For example, to delete line 10 of the program, type

10  RETURN

Now list the program by typing

LIST  RETURN

And you will see that line 10 has been deleted. Now type the original line 10 again so that it goes back into your program.

The second method of deleting program lines is to use the DELETE command. This command allows you to delete a number of consecutive lines.

To do this, type DELETE, then type the first line number to be deleted, then a comma, followed by the last line to be deleted. The following examples will help you understand, but before typing them into the computer, remember that they will in fact delete parts of your program, so afterwards you will need to type the deleted lines again to restore your program!

First of all, list your program so that you can see it on the screen. You should have lines 10, 20, 30 and 40. If not, then replace the missing ones (copy them from the program listing at the beginning of this chapter).

To delete lines 10, 20 and 30, type

DELETE 10,30  RETURN

To delete all lines from the beginning of the program to a particular line number, type 0 followed by a comma followed by the last line to be deleted. For example, to delete lines 10, 20 and 30, type

DELETE 0,30  RETURN

To delete all lines from a particular line to the end of the program, the numbers to enter after the DELETE command should be the first line number to be deleted, then a comma and then any number which you know to be equal to or greater than the last line number of the program. For example, to delete line 20 onwards, type

DELETE 20,100  RETURN

Inserting New Lines into your Program

First of all, list your program (if you haven't deleted it all!) and type in any lines which may be missing.

Having typed in the first attempt of a program, executed it by using the RUN command, and changed or deleted lines as necessary to make the program work, you may want to insert new lines. You will then see how important it was to leave plenty of unused line numbers between the original lines in your program. To insert new lines, decide when you want the new line to be executed by the computer when it runs the program, then choose a suitable line number. For example, to insert a line which makes the computer print another message underneath GIVE ME A NUMBER FROM ONE TO TEN, you will need to insert a new line somewhere between lines 10 and 20. Let's choose 15; this still leaves some room either side for any more new lines, so now type

15 PRINT "NICE DAY ISN'T IT?"  RETURN

Now list the program, and you will see that the new line appears in the listing. The listing should look like this

10 PRINT "GIVE ME A NUMBER FROM ONE TO TEN"
15 PRINT "NICE DAY ISN'T IT?"
20 INPUT X
30 Y=2*X
40 PRINT "TWO TIMES ";X;" IS ";Y

Make the computer execute the program by typing

RUN  RETURN

and type a number for it to multiply by two. Remember that the computer is expecting a numeral and will not recognise letters. This is because our little program would need some more lines adding to it before the computer would recognise THREE instead of 3, or TEN instead of 10.

Renumbering The Program

There may be occasions when you want to change the line numbers of a program but without changing the order in which they are executed by the computer. The command which does this is RENUMBER. This facility is especially useful when you want to insert say 25 new program lines between lines 10 and 20 in your existing program.

You can specify two numbers after typing the RENUMBER command. The first number tells the computer what you want the first program line number to be changed to, and the second number tells the computer how much to add to each line number to get the next one.

For example

RENUMBER 100,20  RETURN

will renumber the first line as line 100, and the remaining lines will be numbered 120, 140, 160 and so on.

If you leave out the second number in the RENUMBER command, the computer will automatically change the second line number to ten more than the first, and then carry on through the listing. So if you had a program with line numbers

23 PRINT "The Electron"
24 PRINT "Microcomputer"
26 PRINT "will do"
30 PRINT "many things for you"

and type

RENUMBER 100  RETURN

listing the program will give

100 PRINT "The Electron"
110 PRINT "Microcomputer"
120 PRINT "will do"
130 PRINT "many things for you"

If you simply type

RENUMBER  RETURN

then your program lines will be renumbered 10, 20, 30, 40, 50 and so on.

Getting the Computer to Number Each Program Line

Instead of typing line numbers at the beginning of each new program line, you can get the computer to do this for you by using the command AUTO.

If you type

AUTO  RETURN

you will see that the computer will print the number 10 on the line below. You can then type the first program line, press RETURN at the end, and the number 20 appears on the next line - and so on. When you want to switch off this automatic line numbering, press SHIFT.

If you don't want to start the program at line 10, and want a different number of spare lines between each of your program lines, then you can type in two numbers - separated by a comma - after the AUTO command. For example if you type:

AUTO 400,15  RETURN

the program line numbers will come out as 400,415,430,445 and so on.

Now press BREAK and retype the example program we were using previously using the AUTO facility. Start the program numbering at 200 and continue in steps of 50.

Putting Notes Into Your Programs

When typing programs, especially long ones, it is a good idea to insert comments here and there to tell you what each part of the program is doing. This is done by using the REM command. All REM does is tell the computer to ignore the rest of the line when it executes the program, but your comments will still appear in the program listing when you give the command LIST.

For example, we could insert a comment at the beginning of our example program to tell us what the purpose of each program is, like 'This program doubles numbers'. To insert this into the program listing, first type

LIST  RETURN

to get a listing of the program. Then choose a line number less than the first program line, say 5. Now type

5 REM This program doubles numbers  RETURN

If you list the program, then run it, you will see that the comment in line 5 appears in the listing, but is ignored when the program is executed.

Retrieving a Program and Starting a New One

If you press the BREAK key for any reason, the program you have typed in so far gets 'lost'. To get it back again, type

OLD  RETURN

If you want to start a new program, make sure that any program already stored in the computer has been deleted. In other words, type

LIST  RETURN

and if a program appears, then delete it by typing

NEW  RETURN

You can now enter your new program, and the old one is forgotten.

Listing Long Programs

When listing very large programs, which won't fit on the screen all in one go, the beginning of the listing will disappear off the top of the screen. There are two ways of getting round this: one way is to press CTRL and SHIFT together as soon as you have typed

LIST  RETURN

The effect of this is to halt the displayed listing on the screen. Taking your finger off either CTRL or SHIFT allows the listing to continue, and this enables you to 'step' through chunks of the listing.

The other method is to put the Electron into 'paged mode'. Press CTRL N to get into 'paged mode', then list the program. The listing stops as soon as it has filled the whole screen. To display the next 'screenful' of listing, press the SHIFT key.

Press CTRL O to get out of paged mode.