R.T.Russell Home Page

Chapter 2 - One Line Programs




One of the nice things about BBC BASIC over other languages like Visual BASIC or C is it is perfectly possible to write one line programs. Type a dozen characters into the editor, press Run and there's your working program. The program will behave like any Window, you can move it, minimize it, resize it - everything you would expect, all with just one line of code. That's pretty powerful and this power lets us experiment with some of the fundamental commands that BASIC provides immediately without three chapters of introductory pre-amble.

Start BBC BASIC. We'll now type some commands in to see how BASIC reacts. Our first command will get the computer to write to the output window. Type the following. Notice how PRINT is in upper case. All BBC BASIC commands and keywords must be typed in this way or BASIC will get upset and complain. It is possible to override this, but let's keep things simple and use the default settings.

PRINT 52
Followed by Enter or Return. Press the Run button from the toolbar, the one with the big black arrow, or press F9.

The computer responds by opening a new window which displays:
52
That's it: your first one line program, not worth sending your CV to Bill Gates just yet but it is a fully functional program. Close the window by clicking the little cross in the top right corner, just as you would any other window. You must always close the window before BASIC will allow you to alter the code in the editor. Place the cursor before the 52 and press delete a couple of times, now retype the line so it looks like this:
PRINT 50+2
Press Run and BASIC responds:
52
Now we're onto something. We can use this as a calculator. Close the window again. From here on we'll take the 'type - run - close window' cycle as read. Again, edit the line so it looks like below, don't delete PRINT, just alter the numbers:
PRINT 50+50-2
Response:
98
Try other mathematical expressions, using + (addition), – (subtraction), * (multiplication) and / (division).
PRINT (50*2)/4+8.1
The answer is:
33.1
So, PRINT will take whatever follows and write it to the output window. More importantly, if the values after PRINT form a mathematical expression, PRINT will calculate them and display the result. Try the next line, taking notice of the quotes:
PRINT "50+2"
Response:
50+2
If we enclose what we want in quotes, it will print exactly as written; hence, we can output text as well as numbers:
PRINT "Hello, world"
If in entering any of the above, BASIC responds:
Mistake
or
Missing "
or something similar, it means you have typed it incorrectly and need to re-enter it. Just in case you haven't seen this yet, go back into the editor and delete the final quote:
PRINT "Hello, world
PRINT is one of the most flexible commands in BASIC and you can do far more with it than output single statements. To begin with, it is possible to print more than one thing at once:
PRINT "50*2 = ";50*2
Gives us:
50*2 = 100
Each separate item must be delimited by either a semicolon (;) or a comma (,). A comma adds more space.
PRINT "50*2 = ",50*2
Gives:
50*2 =          100
PRINT on its own gives us a blank line. If we want several blank lines, BASIC allows us to put a single quote ( ' ) after the initial PRINT. Each quote will produce a blank line.
PRINT '''
or
PRINT "Hello" '' "World"
Each statement you have seen so far tells BASIC to print on the next line down. You can tell BASIC to print anywhere on the screen. PRINT will work in conjunction with certain other keywords one of which is TAB. TAB comes in two guises. With the first, it is supplied with one parameter (or argument as programmers like to call them). This will shift the output along a number of spaces:
PRINT TAB(10);"Hello"
 
           Hello
The brackets here are important. The first bracket must be immediately after the word TAB (no spaces) and the closing bracket must be after the number, although calculations are allowed:
PRINT TAB(5*2);"Hello"
The closing bracket should always be followed by a semicolon.

The second variant of TAB takes two parameters and allows us to print anywhere on the screen.

PRINT TAB(10,20);"Hello"
The first parameter is the column (across the screen left to right), the second is the row (down the screen top to bottom); both start counting at zero. If you imagine a grid overlaying the window, each square in the grid can contain one character. The TAB statement tells BBC BASIC which square to write the first character in. The others are added to the right of this. If the text you print exceeds the length of the line, the text wraps round to the start of the next line and if this occurs on the bottom line, the whole window will scroll up if necessary.

It is possible to get the program to run more than one statement on a line by separating each statement with a colon ( : ). This works a bit like a full stop in English and effectively tells BASIC, that command has now finished, here's the next.

PRINT TAB(10,20)"Hello" : PRINT TAB(10,21)"World"
There is a wonderful version of Tetris in the examples that come with BBC BASIC. It is all written in one line using this technique. As an intellectual exercise and a bit of fun, that's fine. Usually we want our programs to be more readable and so we need to expand into multi-line programs.

Left CONTENTS

CHAPTER 3 Right


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