Variables
So far we have only told the computer to print exactly what we typed in within quote marks. However, the main purpose of a computer is to hold information and data which we would find too much to remember all at once.
You have heard computers mentioned in terms of K (or these days, M). For example, 128M, 256M etc. This is how much memory the computer has. Each K is actually 1024 (1000 in binary, or base 2) characters, or in computer language, bytes. The K stands for kilo, but these days you're more likely to find memory referred to in Megabytes or M. This is 1000 K or a million characters. You can imagine a computers memory as very much like an office 'pigeon hole' system. Each pigeon hole can be given a name (like names of people in the office) so that the computer can refer back to it when required.
These 'pigeon holes' are called variables in computer language and can be given any name you like. The only, slightly tricky thing to remember is that, as computers are very thick, you have to tell the computer whether each variable is going to contain text or numbers. The difference here is that if a variable contains text it's usually just plain text and treated as such. Whereas if a variable contains a number, you can get the computer to do all sorts of things to it, such as add it to another variable (which is also numeric), or subtract it, or square-root it. In fact, computers can do most things that your pocket calculator will do.
The easiest way to show how computers store their variables is to try a couple of examples;
Type
A$="Hello there." <return>
followed by
A=10 <return>
You have just created two variables; one called A$ and one called A. The first is a text variable - usually called a string variable. The second is a number, or numeric variable. How can you tell the difference? Well, each string variable name ends with the $ (dollar, shift '4' on the keyboard) character. This tells the computer that you want to store some text in a pigeon hole and call the pigeon hole A$.
To store a number, you don't use the dollar symbol and just type the variable name on its own. Variable names can be any sequence of characters you like - sbject to a few simple rules. Each variable name must begin with a letter. Numbers are not allowed as variable names, although you could use a name such as A1 if you wanted to.
You should also avoid any of the special symbols such as #, (, @ etc. As a rule, just use the letters A-Z or a-z. Variable names can be upper or lower case, or even a mixture of both.
Try typing the following:-
mynumber=6 <return>
This has now created a numeric variable called 'mynumber' and stored the value 6 in it. To prove it, type in PRINT mynumber <return> and the computer should go off, look in the pigeon hole called 'mynumber', find the value there and PRINT it to the screen.
Try the following:-
PRINT mynumber+A <return>
You should get the answer 16 printed on the screen. The computer has gone off to find the variable called 'mynumber' and added it to the variable called A (which we created earlier). It's then printed out the result.
You can even create another variable to store the result in if you like. For example, type
result=mynumber+A <return>
If you now type PRINT result <return> you will see that the computer has added together the two variables and stored the result in another variable, called 'result'.
Arithmetic and simple sums
The computer knows how to multiply, add, divide and subtract, in addition to a collection of other useful functions. To add and subtract, the computer uses the same symbols as you and I: + for adding and - for subtracting. However, when it comes to multiply and divide, the computer uses slightly different symbols due to the keyboard not having a divide symbol and using an x for multiply could be confused with the small letter x. So, instead the computer uses * (a 'star', shift '8' on the keyboard) and a / (a 'slash', immediately to the left of the right-hand shift key) for multiply and divide respectively. Try multiplying two of your variables by typing:
PRINT mynumber * A <return>
You should get the answer 60 displayed. Note that you don't have to type spaces on either side of the * in the line above. Although, if you do, it will make your program easier to understand and look less confusing.
Some other arithmetrical functions have to be typed in as BASIC keywords, such as sqauer roots. To find the square root of a number, type the following:
PRINT SQR(mynumber) <return>
You should see the answer 2.4494974 displayed.
Other functions you can try are:-
COS to find the Cosine of a number (in radians)The computer also knows about PI, which is stored as the value 3.141592653 inside the computer. For example, try the following:-
PRINT PI <return>If you get into the habit of using lower case letters for variable names, you will always avoid confusion with any BASIC keywords, which MUST ALWAYS be upper case. You can't call variables by any names that are the same as BASIC keywords, so you can't create a variable called SIN or PI.
As the computer doesn't distinguish between spaces, you can't even have a variable called PICTURE, or one called SINGLE - can you see why? They both start with BASIC keywords which are reserved for the computers use only. The first contains PI and the second, SIN.
However, as the computer doesn't recognise lower case letters as BASIC keywords, you are free to use variables such as 'picture' or 'single'.
Writing a program where all the variables are determined at the time you type in the program is not much use though. Wouldn't it be better to be able to type in values when you run the program? That way, you could write a short program to do a calculation and then display the result for you.
The BASIC keyword to do this is INPUT. We need to tell the computer where to store the result, so we use another variable; either numeric or string. For example:-
INPUT A$ <return>will tell the computer to wait for you to enter some text and, when you press <return> will store it in a variable called A$.
INPUT store <return> will wait for you to type in a number and press <return>. The number will then be stored in a variable called 'store'.
If you want a prompt for your input, you can add text to either form of INPUT, simply by inserting some text inside quote marks BEFORE the variable name. For example:
INPUT "What is your name ?" name$ <return>
Will ask the user for their name, which they can type in from the keyboard. The computer will then store their answer in a variable called name$. As most uses of the keyword INPUT are asking the user something, you can get the computer to automatically provide you with a question mark by using a semi-colon between the text and the variable name, for example:
INPUT "What is your name ?";name$ <return>
Don't forget to leave a space at the end of your text string, so that your reply is not positioned right up next to the 'e' of name.
Entering your first program
To illustrate all the things we have learnt so far, we shall now write a program to convert temperature values from centigrade to fahrenheit.
Type the keyword NEW <return> which will delete any existing program and variables from te computers memory and prepare the computer for you to start typing in a fresh program. You should always type NEW <return> before entering a new program, so that any lines you type will not be confused by the computer with any previous lines that may have been entered before you started your program.
before you start writing a program, it's always best to sit down and think about how you are going to solve the problem. This way, you can jot down a few ideas about what you want the computer to do, and what results you want from it.
Firstly, we want the user to enter the centigrade value, so we can start with the following line;
10 INPUT "Enter degrees centigrade ";centigrade <return>
When run, the user will be prompted to type in a numeric value, which will be stored in the variable 'centigrade'. We can now add the following line to convert the value into fahrenheit.
20 fahrenheit=(centigrade*9/5)+32 <return>
Notice that we have put brackets around part of the formulae. The reason for this will be explained in a moment. Firstly, we shall add the last line of our program - outputting the result of our calculation.
30 PRINT centigrade;" degrees C equals ";fahrenheit;" degrees F" <return>
Notice how we have mixed variables with text to provide a clear and informative output. Make sure that you don't miss out any of the quotes or semi-colons!
Now type RUN <return> and enter a value. Press <return> again and hey presto, the answer.
Algebraic Logic
That may sound like a complicated title, but algebraic logic can be quite simple to understand, once you get the hang of it. As mentioned earlier, some formulae need brackets around the numbers in order for the computer to give the correct answer. This is because computers calculate certain mathematical operations before others. For example, computers do multiplication and division before they do addition and subtraction. In turn, they calculate powers before doing multiplication and division. Algebraic logic is simply the name given to this method of calculating numbers. Most modern calculators also use the same method.
Lets look at some examples; If we write down 2*8+4, the computer will interpret it as (2*8)+4, giving an answer of 20 (and not 24, which is 2*(8+4)). This is because the computer will do the multiplication first.
What do you think the computer will give as the answer to 2+8*4 ?
If you wanted the computer to give 24 as the answer, then you can add extra brackets where required, so that the computer will work out the brackets first. Type PRINT 2+(8*4) <return> to see for yourself.
If we were to read it aloud, we may think that the number would be two plus eight (which is ten), time four, giving forty. Incorrect! Using algebraic logic, the multiplication is done first, giving 8*4=16. This is then added to 2, giving an answer of 18.
In BBC BASIC, we can raise a number to a power by using the ^ symbol (shift 6). For example, to find the square of 6, we can type PRINT 6^2 <return> and to find the cube of 9 we can type PRINT 9^3 <return>.
As powers are done before multiplication, see if you can work out what the computer will give as an answer to the following sum;
PRINT 4*7^2+3 Make a guess and then press <return>.
The answer is 199. Were you correct? Lets look again at how the computer works this out. Firstly, the computer will work out the power, ie. 7^2, or 7 squared, giving a result of 49. Next, the computer will carry out the multiplication, which is now 4*49, giving 196. Lastly, the computer will add the 3, to bring the answer to 199.
See if you can work out what results will be given to the following sums. To find out if you are correct, try PRINTing them on the computer.
1. 8/3+4*2
2. 3+3^3/3-3
3. (2+4*2)+2^(8-2)
4. 8-2^2+1
5. 7-2^3
If you are uncertain how the computer will calculate a given sum, you can always put extra brackets in, to force the computer to work out sums in any order you require.
To find out how to save and load your programs, continue to lesson 3.
|
Last edit: 11th Dec 2009 at 12:49pm |
| |||||||||||
|
| ||||||||||||||||||||||||