Are you one of those people who would really like to learn to write a useful program, but has never quite managed it to date? Perhaps you haven't had time, or you haven't had the right books. Somehow you never managed to get started. Well, no more excuses! This series of tutorials starts from the very beginning, but will take you right through to writing your own wimp program. All you need is already there in the machine. We will be using BASIC as the language, and Edit to do the writing!
If you have never programmed before, try this little exercise. Load Edit onto the iconbar, and bring up the iconbar menu. Apart from Info and Quit, it also has Create and BASIC options.
Opening a BASIC window in Edit'
Follow BASIC options and make sure that Strip line numbers is ticked. Now go to Create in the menu, follow it across to the right and click on BASIC. An Edit window opens that looks no different to any other Edit window, but when we come to save this you will find that it claims to be a BASIC file.
Now write the following lines in this window, taking care to do so exactly in every detail.
A$="Acorn User"
C%=0
REPEAT
PRINT A$
C%+=1
UNTIL C%=5
END
Make sure that you pressed RETURN after 'END'. Save the result as Prog1.
Note that Edit saves in the correct BASIC filetype
Now double-click on Prog1 and see what happens. If all goes well, a window opens on screen, prints Acorn User five times, and then asks you to press SPACE or click mouse to continue. When you do so, the screen clears and the window is deleted.
This very simple BASIC program contains a number of features, but before we explain them, look at the program again and ask yourself whether you can work out what is happening, and why it works as it does.
Like most programming languages, BASIC is made up of a series of statements, or instructions. To be understood by the computer, these must follow certain rules. In the English language we would refer to the rules as grammar; in a computer program it is more often referred to as syntax.
For the moment, each statement will be written on a separate line. In BBC days, several statements were written on one line to save space, but these days programmers avoid this most of the time in order to make it easier for other programmers to follow their thinking.
In our program, A$ and C% are called variables. A variable has a name, and it has a value associated with it. The first variable is called A$, and the first line of the program gives it the value "Acorn User". Because it contains letters rather than a number, it is called a string variable. All string variables must end with $, so other string variables might be called B$, Albert$ or name$, but B, Albert and name cannot be string variables.
C% is given the value 0 in the second line of the program. C% is called an integer variable because it can only represent whole numbers. This is indicated by the % symbol.
In line 1 we have given the variable A$ the value 'Acorn User'.
In line 2 we have given the variable C% the value of zero.
Line 3 is the keyword REPEAT. There are a large number of keywords in BASIC which together make up the instruction set for the language. We will only introduce these as we need them, but if you are really curious I will be telling you how you can get them up on screen at the end of this tutorial! Meanwhile, REPEAT tells the program to repeat the next sequence of instructions as far as the keyword UNTIL for as many times as required. The lines to be repeated in our program are lines 4 and 5.
Line 4 is PRINT A$. PRINT is another keyword, and tells the program to write the value of A$ on the screen. In other words, it tells the computer to write the words 'Acorn User' on screen.
Line 5 is C%+=1. This is a very useful shorthand. '+=' tells the computer to add 1 to the value of C%. (In earlier versions of BASIC this had to be written as C% = C% + 1, which was much more clumsy!) In our example, the first time it meets Line 5 it will change the value of C% from 0 to 1.
In line 6 we reach the UNTIL statement, and the condition is 'UNTIL C%=5'. On the first time round, C% is only 1, so the program begins the REPEAT loop again. On this second loop, it will print 'Acorn User' for the second time, and C% will be changed from 1 to 2. This will be repeated until C% eventually reaches the value of 5 and the loop ends.
Line 7 has a very simple keyword - END! In other words, this is the end of the program, so stop.
So far, we have introduced three fundamental ideas.
We have also seen that the equal sign '=' is used to give values to variables, and that we can extend this to change an integer variable by a set amount by using the '+=' format. We can also use '-=' in the same way to reduce the value of a variable. Thus if D% starts off as 3, and we have D%-=5 then D% would now have a value of -2.
Our plan is to write a WIMP program - one that will run in its own window just like any other desktop program, with an icon on the iconbar, and serving a useful purpose. Our program will eventually enable you to store information on your computer for quick and easy reference, and is based on a program I wrote a couple of years ago to store things like PIN numbers - the one we all forget so easily, but have to keep secret from prying eyes. Not surprisingly, it is called Secrets, and if you are impatient you will be able to try the Freeware version on the CD before we complete the course! There is no way we can cover all the necessary ground in the limited number of tutorials we have in mind, so we shall be taking some shortcuts, but nevertheless by the time we finish we hope you will have a good idea of how to program in BASIC and be able to experiment successfully for yourself.
Here are some simple exercises to try based on the above program.
What happens if you add another line within the REPEAT loop in the form of
PRINT C%
or PRINT "A BASIC program"
or even just
Try using the '-=' construct and change the starting value of C% so that it counts down from 5 to zero instead of counting up from zero to 5. Remember that you will have to change the UNTIL line!
What would happen if you use a floating point variable C instead of C%? And what if C starts with a value of 0.1? (Get ready to press the Escape key on this one!) Can you think of a way round this problem? If you think you can, try it.
Finally, if you want to look at all those BASIC keywords, press F12 and type the following:
BASIC
HELP .
QUIT
and press RETURN to return to the Desktop. (Yes, that is 'space fullstop' after the word HELP!) 161 keywords if I am not mistaken, and yes, I have to admit, there are some there I have never even thought of using myself!
(Important note for those with older machines:
Edit did not always have the facility of writing BASIC code without line numbers. If your version does not have this, and you do not have an alternative text editor such as DeskEdit or Zap, then you will need to work outside the desktop. to do this, press F12, then type in the word BASIC. You now type the code as given in the article, but to run the program type RUN. When you want to finish with BASIC type QUIT followed by RETURN to return to the desktop.)