ZX SPECTRUM Introduction |
4. Some simple commands The computer memory can be used to store all sorts of things. We have seen, so far, that the PRINT command allows us to show letters, numbers and the results of calculations using both letters and numbers, on the screen. If we want to tell the computer to remember a number, or a string of letters, then we have to allocate some of the memory for that use. Most pocket calculators have a key called 'memory' which is used for remembering numbers for later. Your computer can do much better than that: it can have as many of these imaginary boxes In it as you like and you write a name on each one. As an example, suppose you want to remember your age! The LET command is used (LET is the keyword on the L key): let's say it is 34 LET age=34 What happens when the LET command is used, is that a certain section of memory is designated 'age' and the number 34 is stored in it. To get this stored information out type PRINT age and back comes the number 34. It is very easy to change the contents of the 'box' called 'age', Type: LET age=56 then type: PRINT age and 56 should appear on the screen. 'age' is an example of a variable so called because its value may vary. It is possible to combine printing a message direct to the screen, and the value of a variable. Type PRINT "Your age is ";age However the computer is a lot more useful than just remembering numbers with names attached to them. It can also remember strings of letters. To differentiate between number variables and string variables - as they are called - the dollar symbol - $ - is used at the end of the variable name. For example: if we wanted to save the string of letters "Your age is" we could call it a$ (string variable names can only have a single letter, other than the $, in them). So type LET a$="Your age is " If you now key PRINT a$ back comes the string of letters on the screen. If the computer hasn't been turned off since the start of this chapter type PRINT a$;age and see what happens. There are other ways of getting information into the computer's memory without using the LET command. For example the INPUT command, in its simplest form tells the computer that some information is expected from the keyboard. Instead of typing LET etc. everytime, you can key INPUT age Once the ENTER key has been pressed a flashing L cursor will appear on the screen. This means that the computer wants some information from you. So type your age and then press the ENTER key. Although nothing seems to have happened the variable has now been given the value you typed in. Typing PRINT age should prove this. Let's combine all this together into a series of commands. Type LET b$="What is your age?" LET a$="Your age is " INPUT (b$);age: PRINT a$;age Note that the last line consists of two commands separated by a colon. INPUT (b$);age is another way of entering INPUT "What is your age?";age |
|
|