R.T.Russell Home Page

Chapter 3 - Using The Editor




The editor allows you to build up a sequence of commands by writing them on successive lines. To demonstrate this enter the following, one line at a time. Notice how when you press Enter, the editor formats the line for you and prints words it recognizes (keywords) in different colours.
REM My first program
PRINT "BBB   BBB    CCC"
PRINT "B  B  B  B  C   "
PRINT "BBB   BBB   C   "
PRINT "B  B  B  B  C   "
PRINT "BBB   BBB    CCC"
END
Select Run, the output window opens up and the message is displayed for all to see.

There are two new keywords here. REM and END. REM has been called the most important word in BASIC. Strange because it actually does nothing. It tells the computer to ignore anything that follows up to the end of the line. This is important as it lets us add text to our programs to remind us what the program does. REMs can be placed just about anywhere in the program and should be, because even the most obvious code in the world somehow manages to obscure itself when left alone for a couple of months. It's also nice for other readers as it explains what the code is intended to do. To enter a comment after a line of code, use the colon to separate the lines, just as described in the previous chapter. All comments are coloured green by default, so you can tell instantly if you are looking at active code or a comment.

The other word is END. This tells BASIC to stop executing the program. In short examples like this, it's not too important, but later on when structure is discussed, it becomes crucial. Get into the habit now of putting it in.

We will now play with the editor a little more, just to get a feel for it.

The larger a program gets, the more likely it is that we introduce mistakes or bugs. As the editor very kindly colours words it knows, it is fairly easy to spot misspelt keywords; a common mistake is forgetting that all keywords must be in capitals. Other typing mistakes or runtime errors are harder to spot. BASIC will point these out for us though. Remove the final quote from one of the lines above and run the program. BASIC screeches to a halt and announces: Missing " when it reaches the offending line. To find the line on which the error occurred, go back to the editor and there it is, nicely highlighted. To edit the code, you need to close the output window or press the Stop button before the editor will let you back in again. Do this now, correct the mistake and then we can play some more.

If you've never run a program before, you might think that all this text appears in one swoop. Not so. BASIC executes the lines, one at a time in just the same order as you would read them, top to bottom. We can prove this by introducing another useful keyword. As computers do things very fast, there are times when it can be useful to slow them down a little. WAIT is an instruction that has no other function than to stop the program in its tracks for awhile. You have to give it a value to work with, which represents the amount of time to wait. Again, as computers move fast, the value is in centiseconds - hundredths of seconds or 0.01 seconds if you prefer. There are 100 of these in a second therefore 5.25 seconds would be 5.25 * 100 = 525. Let's edit our program so we can see it working. Place the cursor at the end of the first PRINT line and press Enter. A blank line is created. Now type WAIT 100 and press the down arrow, not Enter because we don't want another blank line. The program now looks like this:

REM My first program
PRINT "BBB   BBB    CCC"
WAIT 100
PRINT "B  B  B  B  C   "
PRINT "BBB   BBB   C   "
PRINT "B  B  B  B  C   "
PRINT "BBB   BBB    CCC"
END
Click the mouse at the start of the WAIT 100 line. Now hold down the shift key and press the down arrow on the keyboard. The whole line is highlighted. Right click the mouse and select Copy. Press the down arrow to move the cursor down a line. Right click and select Paste. Our text is copied into a new line, saving typing. Move the cursor down and repeat until the whole program looks like this:
REM My first program
PRINT "BBB   BBB    CCC"
WAIT 100
PRINT "B  B  B  B  C   "
WAIT 100
PRINT "BBB   BBB   C   "
WAIT 100
PRINT "B  B  B  B  C   "
WAIT 100
PRINT "BBB   BBB    CCC"
END

When the program is run, it appears one line at a time with a one second gap between each line. To make our program behave as it did before, we can delete the wait lines. Before we do that, highlight the first WAIT line and right click. Select Add REMs from the menu and the line is commented out. Repeat this for the other lines.

REM My first program
PRINT "BBB   BBB    CCC"
REM WAIT 100
PRINT "B  B  B  B  C   "
REM WAIT 100
PRINT "BBB   BBB   C   "
REM WAIT 100
PRINT "B  B  B  B  C   "
REM WAIT 100
PRINT "BBB   BBB    CCC"
END
As REM causes BASIC to ignore everything else on the line, the WAITs are now bypassed and the program runs as before. Using REMs like this is quite common when testing a program to block out lines of code. It is perfectly possible to REM lots of lines by selecting more than one at a time. You can remove the REMs in a similar manner by highlighting lines, right clicking and selecting Remove REMs.

To delete the lines completely, place the cursor at the start of the line, hold down shift and press the down arrow once. Press the delete key and the line disappears. The editor shuffles the remaining lines up to fill the gap. Delete the WAIT lines now, you'll need the original program for the exercise later on.

Programs grow in size, almost as you look at them. An important thing to bear in mind with programs of any size is readability. BBC BASIC for Windows allows us a couple of useful techniques to add space to our program and make it easier on the eye. Neither of these alters the way BASIC runs the code, but makes it easier for us poor humans to follow.

You can include blank lines in your programs. They are ignored by BASIC but do make things easier to read. Our little program could be easily written:

REM My first program
 
PRINT "BBB   BBB    CCC"
PRINT "B  B  B  B  C   "
PRINT "BBB   BBB   C   "
PRINT "B  B  B  B  C   "
PRINT "BBB   BBB    CCC"
 
END
It would run exactly the same, but just looks that bit clearer.

BBC BASIC allows up to 251 characters on each line, but when lines get this long, they can be a pain to read as it involves scrolling across the screen. It is possible to split a line using the backslash ( \ ). To indicate that the line is incomplete, put the \ at the point you want to split the line. The next line must start with \ as the very first character to indicate this is the continuation. As BASIC ignores everything after the \ at the end of the line, you can insert comments there if you want.

Tip: Splitting long PRINT lines
You may remember from the first section that we can use a semicolon to print items next to each other. We can use the semicolon to split a line with lots to print on it like this:

REM Splitting long lines
PRINT "This is a very, very, very, very,"; \
" very, very, very, very, very, very,"; \
" very long line."
END

Exercises

1) Modify the first program to read "BB4W" instead of "BBC".
2) Insert TABs into the print statements to print BB4W starting at row 10, column 10 in the output window.

Left CONTENTS

CHAPTER 4 Right


Best viewed with Any Browser Valid HTML 4.0!
© Peter Nairn 2006