Miscellaneous commands and adding data.
GET - Waiting for a key to be pressed.
You may hve noticed a new keyword mentioned in the examples in lesson 6; GET
GET is a built-in computer function and works a bit like a variable, but instead of defining it yourself, the computer defines it as whichever key you press. In fact the computer will do just that - wait for a key to be pressed.
There are two forms of GET. A numeric version and a string version, called GET$. The first returns the ASCII value of the key pressed, whereas the second returns the actual key itself, as a string.
You can assign it to a variable (as in the last example) by using a line such as;
key$=GET$
This will wait for a key to be pressed and then make key$ equal to the letter corresponding to the key. For example, if you pressed G, key$ would contain "G". By using a numeric variable such as;
key=GET
The computer would still wait for a key to be pressed, but would then assign the variable key with the ASCII value of the key pressed. eg. If you pressed the "A" key, the value 65 would be placed in variable key. Quite a common use of GET is to wait for the space bar to be pressed. Both the following lines would wait for you to press space.
REPEAT UNTIL GET$=" "or
REPEAT UNTIL GET=32
Reading and Using Data - READ and DATA statements
There may be times when you want the computer to manipulate a set of fixed data values such as looking up a chemical element from a Periodic table or colour codes from a paint card. There are two statements; READ and DATA to do this for you. READ enables the computer to look for a DATA statement and read a value from it. The next time the computer sees a READ statement, it will read the next value from the data statement. The following example will help to clarify;
10 PRINT "Here's some countries of the world." 20 FOR N=1 TO 10 30 READ name$ 40 PRINT name$ 50 NEXT N 60 END 70 DATA Afghanistan,Argentina,Australia,Austria 80 DATA Belgium,Brazil,Canada,China,Czechoslovakia 90 DATA Egypt
The loop at lines 20 to 50 will read in 10 names from the list starting at line 70. The only thing you must remember is not to run out of data. For example, if you change the 10 in line 20 to 11, then the computer would give an "Out of data" error because it would try reading 11 items of data when you have only supplied 10. You would either have to change it back to 10, or type in another country.
One of the most important uses of DATA statements is that of looking up a value within a set of data in order to supply other information. For example, we could modify the previous example to include the capital cities of the various countries.
The user could then be asked to enter a country and the computer would supply the capital. Lets look at the example now. read through it and see if you can work out how it works before reading through the explanation afterwards.
10 INPUT "Enter a country ";c$ 20 REPEAT 30 READ country$,cap$ 40 UNTIL country$=c$ OR country$="xxx" 50 IF country$<>"xxx" THEN 60 PRINT "The capital of ";c$;" is ";cap$ 70 ELSE 80 PRINT "Sorry. I don't know about that country." 90 ENDIF 100 END 110 DATA Afghanistan,Kabul,Argentina,Buenos Aires 120 DATA Australia,Canberra,Austria,Vienna 130 DATA Belgium,Brussells,Brazil,Brasilia 140 DATA Canada,Ottawa,China,Beijing 150 DATA Czechoslovakia,Prague,Egypt,Cairo 160 DATA xxx,xxx
Notice how we have used a REPEAT ... UNTIL loop, to loop until the country read from the data list matches that prompted at line 10. Can you see why we have also tested for xxx?
This is so that, as we haven't told the computer how many countries are in the list, it can stop when it gets to the end of the list, signified by xxx.
|
Last edit: 11th Dec 2009 at 12:49pm |
| |||||||||||
|
| ||||||||||||||||||||||||