Introduction
This short course is designed to help you to get to grips with writing your own short programs on RISC OS computers, using BBC Basic.
BBC Basic was developed over 10 years ago for th original BBC Computer. Since then it has bee re-written and enhanced many times. The version supplied with RISC OS extremely powerful, yet still flexible and easy for newcomers to understand.
In order to work through this course, you should be sitting in front of the computer, with the computer displaying the desktop. ie. the standard desktop display with the iconbar at the bottom of the screen.
To enter BASIC so that you can start programming, press function key f12 (towards the top right of the keyboard), the type the word BASIC and press the return key.
The computer will now enter BASIC and present you with a > (greater than) prompt. This > prompt means that the computer is waiting for you to type something.
N.B. When you see the word <return> in the course text, it means that you should press the return key.
First Beginnings
Type the word CLS <return>. The screen will now clear to black. This is one of the most simple BASIC commands. A command tells the computer to do something - an order if you like.
Another command is LIST. Try it now; Type LIST <return>.
What happened? Nothing? Not exactly. LIST tells the computer to list the program currently in its memory. As we haven't typed one in yet, you get the > prompt straight away as there is no program to list.
Type in the following:-
10 PRINT "Hello there." <return>
PRINT is another command. It tells the computer to output something to the screen. In this case, the text "Hello there.". Notice how we put quote marks around the text. These tell the computer to output literally whatever is between the quotes. It can be numbers, text or even a mixture of both. In fact any character that you can type from the keyboard can go between the quotes.
What happened when you pressed return? The computer didn't print anything did it? This is because we put a 10 before it. This is called a line number and is how we start to write a program. If you now type LIST <return> you will see the line listed for you. You have now written your first BASIC program. It wasn't that difficult after all was it?
Each program line must begin with a number. We usually start at line 10 and go up in steps of 10. You don't have to do this but it is programmers convention and allows you to insert more lines later if you wish.
The computer will always run the program in numerical line order. This doesn't have to be in the order you type them in, which gives you the ability to go back and edit your program at a later date. For example, type
5 CLS <return>
Now type LIST <return> again and see what happens. The computer automatically lists the program lines in numerical order.
Having typed in a couple of lines, how to we make our program actually execute, or run? The answer is simple really. We use another command; RUN - this is quite self-explanatory and tells the computer to do exactly that - run the program.
Can you see what the computer has done? Firstly, it has cleared the screen (line 5) and then it has printed "Hello there." on the screen. Notice that the quotes didn't come out. This is because the computer looks at the quotes and outputs everything between them.
Try making your program a bit more personal by adding line 20;
20 PRINT "<your name>" <return>
Type RUN again and watch what happens this time. Did it behave as you had expected?
Each time BASIC encounters a PRINT command, it starts a new line, which is why "Hello there." appeared on one line and your name appeared on the line below. It would be much nicer to print it all on one line.
BASIC allows us to do this by placing certain characters after the one we want printed. For example, re-enter line 10 by typing;
10 PRINT "Hello there."; <return>
Notice that this time we have added a semi-colon to the end of our line. Remember to place it after the closing quotes though. Do you know what would happen if you put th semi-colon inside the quotes?
A semi-colon will tell the computer not to give a new line after the PRINT command. You can also use a , (comma) which will jump to the next tab position across the screen. The computer automatically divides the screen into a number of vertical tab positions. These are usually spaced every 10 character positions. Thus, if you add a comma to the end of your PRINT command, any text following will appear in the next tab position. The effect of this will vary depending upon how long the text is that you are printing. Experiment with different words.
Editing program lines
You will have noticed from the examples given above, that when we wanted to change line 10 we re-typed the whole line. This could be time wasting and be inconvenient if we only wanted to make a minor change.
Acorn computers allow us an easier method of editing program lines by using the four cursor 'arrow' keys to the right of the main keyboard. These are called CURSOR keys. You will have noticed from typing in the program lines above, that just ahead of where you are typing is a small flashing line. This is known as the cursor, or 'caret'.
By pressing the cursor keys you can move the cursor around the screen. Try it for yourself now. You'll notice that as soon as you press one of the cursor keys, the cursor splits into two. One is still the flashing line, which you can move around the screen. The other is a solid square which remains in its original 'start' position. Move the flashing cursor around the screen to get the hang of it. When you have finished playing, hit the <return> key once. The computer will now issue a > prompt and the cursor will jump back to its new position just after the prompt.
Type LIST to see your program lines.
Now, from the > prompt, keep pressing the cursor keys until the flashing cursor is beneath the 1 at the beginning of line 10.
If you now press the COPY key (immediately above the cursor keys, and called 'end' on some keyboards) you will see that the computer 'copies' the character at the flashing cursor position and places it by the solid cursor position. This is known as copying a character.
Keep pressing COPY until you reach the 'e' in hello. Don't actually copy the 'e'. If you do accidentally copy the 'e', simply press DELETE (to the left of COPY) to remove it. Now type 'i' and press the right cursor key four times so that it is underneath the space dividing the two words. Now press COPY again until you have copied the end semi-colon.
You should see the following text on the screen by the square, non-flashing cursor.
10 PRINT "Hi there.";
Press <return> to accept the change and enter the line. Now type LIST and you'll see that the line has changed accordingly.
You have effectively typed in a new line 10, but by using he COPY key you can avoid re-typing the characters and thus save time. Experiment with editing a few times. Try changing line 20.
Don't worry if the computer says 'Mistake' or 'No such variable' if you press <return>. That probably means that the computer has misunderstood something, usually caused by pressing <return> to execute a command that the computer doesn't know about. These are called BASIC errors and will be explained later. For now, just ignore them. They don't cause any harm to the computer.
By now you will have learnt how to clear the computers screen, print some text, enter a simple program and how to edit your program. Before reading further, make sure you have understood everything so far. If not, go back and try some more experimentation.
|
Last edit: 11th Dec 2009 at 12:49pm |
| |||||||||||
|
| ||||||||||||||||||||||||