Back To Basics - Part 1

What is a program? How do you type it in? And how do you save it? Mark Moxon starts his tutorial in Basic programming with some basic concepts

Welcome to the first part of this series on learning to program in BBC Basic V, the language that comes free with every 32-bit Acorn machine. Although Basic is regarded as a rather slow and primitive language on other platforms, Acorn's implementation is very flexible, very fast, and is ideally suited to those learning to program.

Why should you want to learn to program? Well, there are a number of very good reasons. You can write programs for your own use; you can read and understand other people's programs, and change them if you want; you'll understand more about how the machine works, and how to get the most from it; and most of all, programming is really satisfying. Ask any hacker: the moment when you finally get a program to work is great. So it's on with the show.

First steps

In this article I'm going to cover a few basic concepts (excuse the pun - go on, you think of another way of saying 'basic concepts'). First off is the age-old question 'what is a program?' Next we'll look at a couple of example programs just to get you in the mood. Then I'll talk about how to type the programs in, how to save and run them, and how to track down any typing errors you may have made, followed by a slightly longer program with a line-by-line description. Here we go ...

The concept of a program is very simple: it's just a list of commands which you give to the computer. Imagine giving directions to your house to a friend over the phone: 'turn left at the lights', 'go over the next roundabout' and so on. A program is just like a list of instructions, but the instructions aren't in English, they're in Basic. When you run the program, the computer follows the steps a line at a time until it reaches the end of the program. Then it stops. Easy.

Let's jump straight in at the deep end and look at an example program. For those of you who have read books on Basic, or who have followed other series, please don't groan when you see my examples. No doubt you will have seen them before, but that's only because the oldies are goldies. I make absolutely no apologies for my taste here.

Listing 1

REM >Listing 1
INPUT "What is your name?"name$
PRINT "Hello ";name$;"."
PRINT "Welcome to the world of Basic."

Listing 1 is a very short program. If you know anything about Basic then you'll notice that there's something missing: the line numbers (have a look at the listings in the yellow pages to spot the difference). In Basic programs each line has a unique number, but in these days of enlightened programming practice the line number is regarded as an irrelevance, so in this series I'm going to ignore them totally.

The reason for leaving outline numbers is that you don't need them: they're a throwback from earlier days when you needed to refer to lines within a program, days which are thankfully gone. Seasoned programmers will know all about the line number debate, and this approach is the best possible way to ensure that good programming habits are learned. If you don't know what I'm talking about, it doesn't matter: when you type in programs from the yellow pages, type in the line numbers, but if they're for this series, don't.

Don't worry if you haven't got a clue what Listing 1 does: I'm only trying to scare you off at an early stage.

Typing programs in

Great, so we've got a program. But how do you type it in? And what happens next? Well, there are a number of editors which can be used to create Basic programs: Risc OS 3 Edit, DeskEdit 2, DBEdit, StrongEd II, Zap and BasicEdit to name but a few. I'm going to be concentrating on using Risc OS 3 Edit as it comes free with your system, but if you feel happier with another editor, fine. Do note that Risc OS 2 Edit can't edit Basic programs, which is another reason to upgrade (as if you needed one).

Now to type in Listing 1. Load Edit onto the iconbar, and press Menu over the icon. Move into the Create submenu and click on Basic; this opens up a normal Edit window, but ensures that what you are creating is a Basic program, rather than a straight text file. Get used to this window: you'll be seeing a lot of it.

Typing in the program is a breeze. Simply type each line as it is listed, and press RETURN at the end of each line.

When you've finished typing in the program, save it just as you would save a text file, by pressing F3 or using the Save option from the menu. That's all there is to it; you've created a Basic program.

Typing Listing 1 into Edit

Problems, problems

You can run your program by double-clicking on your Basic file. Hopefully a little screen will appear with a prompt for you to enter your name: do so and press RETURN. The program displays a friendly greeting, and a message appears inviting you to press the space bar, or click a mouse button. This is what happens with programs that do not multi-task: they take over the machine until they have finished executing, then you have to click the mouse to return to the desktop. The programs in this series will not be multi-tasking desktop applications: the theory behind writing multi-tasking programs is complex and books on the subject require prior knowledge of Basic programming.

If you've managed to mistype anything in the program, then the above may not happen. Say you typed PRIT instead of PRINT in the fourth line; running the program would still ask you for your name, but would simply display 'Mistake', followed by the `click mouse' prompt. Try it and see. Tracking down errors in small programs like this example is not too tricky, but imagine a program with hundreds of lines; an error like `Mistake' is hardly helpful. Enter Listing 2.

Listing 2

REM >Listing2
INPUT "What is your name?"name$
PRIT "Hello ";name$;"."
PRINT "Welcome to the world of Basic."

Listing 2 includes one extra line, starting with ON ERROR, and it has a deliberate error in the fifth line. When you type in this program, take very special care to get the ON ERROR line right, as a mistake here could cause your machine to hang - so make sure you haven't got any unsaved work before you run the program.

When you run the program, type in your name, and the error message shown is 'Mistake at line 5'. This may not seem much more helpful than just 'Mistake', but the line number allows you to pinpoint the mistake. To correct the error, use the Goto function in Edit to go to line 5 (via the menu or by pressing F5)and the caret will go to the incorrect line. Change PRIT to PRINT, and the program works fine. If the number in the error message isn't 5, make sure that the entry in the Line number increment submenu is 10.

This system enables you to track down typing errors. All the programs in this series will have the magic ON ERROR line, so it's worth adding a few of your own mistakes to the example pro- grams and seeing if you can find them using F5. Trendy editors like DBEdit track errors and jump to the offending line automatically, but for those without, Edit is perfectly adequate.

I'll use this method of referring to specific lines in programs, so if I refer to the 143rd line, then to get there, press F5 and enter 143.

Line by line

Listing 3 is a slightly longer program with lots more commands to bemuse and amuse. Type it in and run it to display multiplication tables: hardly mind-bending, but not bad for the end of the first article, surely?

Listing 3

REM >Listing3
  INPUT "Which table would you like to see (1-12)? "table%
UNTIL table%>=1 AND table%<=12
FOR loop%=1 TO 12
  PRINT loop%;" x ";table%;" = ";loop%*table%
NEXT loop%

Just so you get a general feel for how a program is executed, here's a quick breakdown of how this third program works, line by line.

1 - A REM statement. This does nothing at all: it just allows us to put little comments in the program (REM is short for remark). Here the comment is simply the program name. Don't worry about the > sign; it's another throwback from ancient days, but it's a convention to put a REM at the start of the program with the program name prefixed by a >.

2 - A colon. This also does nothing on its own, it's just here to space the program out a bit. Attractive, don't you think?

3 - A tough line, this one. It means 'if there is an error then report it, print the line number, and then terminate the program.' Obvious, really.

4-6 - This is called a REPEAT-UNTIL loop. If you were able to describe programs in English rather than Basic, these lines would read: 'Get the user to type in a number, with the prompt "Which table would you like to see (1-12)?" and make sure that the number entered is between 1 and 12. If it isn't, ask again.' The middle line is indented to show that it is inside the loop, purely because it looks nice.

7 - This forms the start of what is called a FOR-NEXT loop. It means that we're about to execute the following lines (up to the NEXT statement in line 9) 12 times.

8 - This is the line that is executed 12 times. It prints up the number of the loop we're doing (which is from one and 12), a multiplication sign, the number the user typed in at the start, an equals sign, and finally the result of multiplying the loop number by the user's number. This, believe it or not, ends up looking like a multiplication table. Again this line is indented as it is inside a loop.

9 - This signifies the end of the 1oop.

10 - This signifies the end of the program.

Don't worry if the above makes no sense; after all, it makes the next instalment more of a challenge. Next month you'll actually start to learn to program in Basic: see you then.

What happens when you run Listing 3

Source: Acorn User - 133 - August 1993
Publication: Acorn User
Contributor: Mark Moxon