Quotes
Type in: PRINT"A"
then press RETURN. The letter A will appear underneath.
Type in: PRINT A
then press RETURN and the computer will print the message:
No such variable
The " marks, called quotes, in the first example told the computer that anything inside the two quotes should be printed as it is.
In the second example there are no quotes so the computer assumed that what came after represented something, called a variable and since the computer hasn't been told about any variables it told you so.
Simple Programs
Type in:
10 A=5 (RETURN) 20 PRINT A (RETURN) RUN (RETURN).The number 5 will appear on the screen.
This is a simple program. In line 10 you told the computer that A=5. The computer now assumes that there is a variable called A and it equals 5.
Then in line 20 you told the computer to PRINT A. The computer assumes that A is a variable and checks to see if it has been told about this variable, it has and so it prints what it believes this variable represents in this case 5.
INPUT
Type in:
10 INPUT A (RETURN) 20 PRINT A (RETURN)
Now RUN the program and a ? will appear on the screen. Press a number and RETURN and that number will appear on the screen.
Here, in line 10 you told the computer INPUT A. The computer took this as meaning wait for a number to be typed in followed by RETURN and assign this number to the variable called A.
In line 20 the computer was again told to PRINT A so it did and printed the number you typed in in line 10.
So far, PRINT tells the computer to try to print what follows.
If what follows is enclosed within quotes then this is printed as it was originally entered. If not it is treated as a variable.
Variables
A variable is something which the computer assumes will have some information which it can act upon such as PRINT. It is called a variable because it can in fact have different information at different times. This is demonstrated using the command INPUT where A is equal to any number you happen to type in.
Mark is GOOD Tom is BAD.
If I say to you who is GOOD, you say Mark. Equally who is BAD, you say Tom.
GOOD and BAD are variables. GOOD is holding the information Mark and BAD is holding the information Tom.
The computer will understand three types of variables, each is a little different with different advantages and disadvantages and each needs to be handled in a different way.
The variable we used above was called a Real variable. It can in fact store numbers and decimal fractions such as 3.14. I gave it the name A yet it can have almost any name.
Type in:
10 INPUT A (RETURN) 20 INPUT B (RETURN) 30 C=A+B 40 PRINT C
and RUN this program. You will see a ? as before from line 10, enter a number, try a decimal number such as 3.14. After RETURN another ? will appear this time from line 20 and you can enter another number. After pressing RETURN the computer will add these two numbers together in line 30 and print the result in line 40. Notice that in line 30 the sum is written with the result before the sum itself. What line 30 is saying to the computer is a variable called C will be equal to a variable called A added to a variable called B.
Operators
To subtract A from B you would use -.
To multiply A with B you would use *.
To divide A by B you would use /.
The symbols for multiply and divide used by the computer are different from those used by us on paper.
Integer Variables
Another variable is called an integer variable. An integer is a whole number, that is one which does not include any fractions. 5 is an integer, 5.25 and 5 1/2 are not because they include fractions.
An integer is indicated by a % sign after the variable name. In common speech, this sign is called percent so A% is...A percent.
In fact, if you wrote the last program using integer variables instead and entered say 3.14 it would be treated as 3 with the .14 being ignored.
Integer variables are processed by the computer more quickly than real variables and can, in some circumstances, be more accurate. More importantly, integer variables beginning with a capital letter of the alphabet such as A%, B%, C% and so on are not cleared when a new program is run or even after pressing BREAK. To mark this special status these particular integer variables are called Resident Integer Variables. This makes then especially useful when several programs need to be loaded one after another and numbers need to be passed from one program to the next.
String Variables
So much for integer variables. The last type of variable is called a string variable. It will hold a string of letters or a single letter. Is is signified by the symbol $ after the variable name. i.e. A$. In this case we would say A dollar.
A string variable doesn't actually do anything mathematical it simply stores what it is told and then delivers it when it is told.
If you type in the last program again this time making each variable a string variable the result will be each of the characters you typed in being printed on the screen. If you enter 2 then 3 the result will be 23. If you enter FR then ED the result will be FRED.
INPUT Again
Now there is another word which has been used called INPUT. This word stops a program and waits for information to be typed in. The input can be almost any length from nothing to 255 characters and once the input has been typed in, the RETURN key must be pressed.
INPUT must be followed by a variable name so that whatever is typed in can be put somewhere to be dealt with later.
GET
Another word for getting a program to ask for information is GET.
This is a particularly easy function to use. It comes in two forms: GET and GET$.
Type in:
10 A=GET (RETURN) 20 PRINT A (RETURN)
When this program is RUN the computer will wait for a key to be pressed. The ASCII code for that key is put into the variable A and, in line 20, A is printed. (Do you know what ASCII codes are?)
Now this is a very useful little word. Suppose there's a list of options for the computer to choose from: 1: Ask for two numbers and add them together, 2: Ask for two numbers and subtract the second from the first, 3: Ask for two numbers and multiply them and 4: Ask for two numbers and divide the first by the second. A simple arithmetic program:
10 PRINT"1.....ADDITION" 20 PRINT"2.....SUBTRACTION" 30 PRINT"3.....MULTIPLICATION" 40 PRINT"4.....DIVISION" 100 A=GET 110 IF A=49 GOTO 100 120 IF A=50 GOTO 200 130 IF A=51 GOTO 300 140 IF A=52 GOTO 400 150 IF A < 49 OR A > 52 GOTO 100 200 PRINT"ADDITION" 210 PRINT"Type in the first number and press RETURN" 220 INPUT B 230 PRINT"Type in the second number and press RETURN" 240 INPUT C 250 D=B+C 260 PRINT"The two numbers you typed in added together equal ";D 270 END 300 PRINT"SUBTRACTION" 310 PRINT"Type in the first number and press RETURN" 320 INPUT B 330 PRINT"Type in the second number and press RETURN" 340 INPUT C 350 D=B-C 360 PRINT"Taking the second number from the first equals"D 370 END 400 PRINT"MULTIPLICATION" 410 PRINT"Type in the first number and press RETURN" 420 INPUT B 430 PRINT"Type in the second number and press RETURN" 440 INPUT C 450 D=B*C 460 PRINT"The two numbers you typed multiplied together equal ";D 470 END 500 PRINT"DIVISION" 510 PRINT"Type in the first number and press RETURN" 520 INPUT B 530 PRINT"Type in the second number and press RETURN" 540 INPUT C 550 D=B/C 560 PRINT"The first number divided by the second equals"D 570 END
You will find it quite easy to extend this program.
The program is in three main parts. The first asks you to choose which type of arithmetic you want to do. A simple menu is printed onto the screen.
In the second section, line 100 makes the variable A equal to GET. GET makes the computer wait until a key is pressed and, in this case, the ASCII value of that key is put into the variable A.
The program checks if this value corresponds to any of the five values in lines 110 to 150 which contain the ASCII codes for the characters 1, 2, 3 and 4. If one of these values is equal to A then the instructions in that line are executed. In this case the instructions cause the program to jump to a particular line for the type of arithmetic you want to do otherwise the next line is checked.
If any other value is put in say 9 or M, then line 150 puts the program back to line 100 and starts the process again.
The second part of the program has been covered already. Notice in lines 260, 360, 460 and 560 that the PRINT command first prints some text then after the closing ", the variable D which contains the total from the problem is printed. This has combined the two methods of print which were described above.
The advantage of using GET in the second, selection part was that with a single key stroke the value is put into A and the program continues. You could have used INPUT here but this would have meant two key strokes.
In the second part INPUT was used because otherwise long numbers couldn't have been tried.
Differences Between GET And INPUT
So the most important difference between INPUT and GET is that GET needs only one key to be pressed but can only take one character. INPUT needs to be completed by pressing RETURN but can have a number of characters.
Another word for getting a program to ask for information is INKEY.
INKEY works in a different way to INPUT and GET. It has two forms. The first is a timed form.
10 A=INKEY(1000) 20 IF TRUE PRINT CHR$ A 30 IF A=-1 PRINT"TOO SLOW"
The number in brackets after INKEY represents the length of time which INKEY will hold up the program and wait for a key to be pressed. This number is infact in 1/100 of a second so in this case INKEY(1000) means wait for 10 seconds.
Line 10 makes the variable A equal to INKEY(1000). If during that time, you press any key the program will put that key's ASCII value into the variable A and move to line 20.
Boolean Values
Another new word in line 20 is TRUE. All computers work in 1s and 0s, yes or no, TRUE or FALSE. Here if a key has been pressed then INKEY will have a value to put into the variable A and so INKEY will be TRUE. If no key has been pressed then INKEY won't be true and the program can't execute line 20.
OK, so it's a little obscure. But you just have to learn it. The people who wrote this did so before the first space rockets. Come to think of it, it was before I was born. Be thankful it was Americans, if it had been British then every word in computers would have been named after some scientist, SMITH BLESSINGTON-SMYTH CARTER JONES$ A somehow doesn't roll so well as IF TRUE PRINT CHR$ A
CHR$
The CHR$ in line 20 will convert an ASCII number to its respective symbol. Try typing in: PRINT CHR$ 101
Computers use numbers to represent each of the characters on the keyboard and a few others besides. If line 20 had said PRINT A then the ASCII code for the key you had pressed would have been printed.
INKEY
If after 10 seconds you don't press any key the program will put the value -1 into the variable A. INKEY is then clearly false and so line 20 cannot be executed. So the program will jump to line 30.
This version of INKEY is interesting but it's uses are a little limited.
The other form of INKEY is much more useful.
This form is followed by a negative number. This number represents a particular key on the keyboard. These are not the ASCII values though for example A is represented by -66 while B is represented by -101.
Now the real advantage of this form of INKEY is that it can be written so that a paticular action will occur when a certain key has been pressed. Setting it up is quite easy provided you have access to a list of the key codes. Also it doesn't matter if you press a capital letter or a small case. These are on page 159 of the Elk's user guide and page 275 of my copy of the Beeb user guide.
REPEAT Loops
10 PRINT" PRESS A" 20 REPEAT 30 IF INKEY(-66) PRINT"WELL DONE" 40 UNTIL FALSE
This program will wait until you have pressed A or a and then print WELL DONE. As it stands it will then continue untill you press A again and print WELL DONE. Not very useful as it stands but the principle is.
Notice in lines 20 and 40 the words REPEAT and UNTIL FALSE. These take us nicely to what is perhaps the most important facility on any computer, the Loop.
In computing there are many times when you need a loop, the last program told you to press A, and when you had done so to print WELL DONE otherwise to continue waiting until you pressed A. The REPEAT UNTIL is a loop which continually cycles through the part of the program between the REPEAT and UNTIL trying to execute the lines between until a condition is met, in this case FALSE when the loop will be broken and the program will continue.
FOR ... NEXT Loops
Another loop is the FOR NEXT loop. This loop will count until a certain figure is reached and then the loop is complete and the program continues.
10 FOR A=1 TO 10 20 READ A$ 30 PRINT"HAVE SOME TEA "A$ 40 NEXT 50 DATA VICAR,TEACHER,MOTHER,FATHER,DOCTOR,REPAIRMAN,SISTER,BROTH ER,DOG,GOLDFISH
Here the loop will cycle ten times, each time executing the lines between. When A is equal to 10 the cycling will stop and the program will continue or end.
Line 20 is a READ which will read information from a DATA line and place it into the variable A$. In a DATA line each item is separated by a comma, and there must be enough items for the size of the FOR NEXT loop in this case 10. Though there can be more this can have some unusual effects if another READ command is encountered later in the program.
Line 30 is a silly question where the person being spoken to is not specified, instead there is an A$ after the closing ". We used this before.
Line 40 is the NEXT command, it, tells the computer to cycle back to the FOR command.
Finally the DATA line.
The FOR NEXT loop really comes into its own when some sort of counting if needed.
10 FOR A=1 TO 255 20 SOUND 1,-15,A,1 30 NEXT
This short program when run will produce a rising tone. Line 10 makes the variable A initially equal to 1. Line 20 is a SOUND command. The third parameter which is the pitch of the sound is set to A, so for the first pass this will be 1, quite a low pitch. Line 30 is the NEXT command which causes the program to cycle back to the FOR in line 10 where A is now equal to 2 and so on.
Gus Donnachaidh, EUG #24