First Steps in Programming
RISC OS Computers
Martyn Fox

Chapter 2 : Introducing Variables

Before we go any further, let's just sort out one or two things.

You've probably grown a little tired of typing in the word PRINT over and over again and wished there was a shorter way of doing it. Fortunately there is!

Both Basic and the command line interpreter recognise minimum abbreviations. This means that you only have to type in enough of a keyword for it to be distinct from any other keyword, then follow it with a full stop (.). If the abbreviation is short for more than one keyword, Basic replaces it with the one which is first on its list, which is usually the most-used one.

The minimum abbreviation for PRINT is P. which certainly saves a lot of typing! Similarly, the minimum abbreviation for MODE is MO.

The command '*Cat' which we used first of all to catalogue the disc has the shortest abbreviation of all:

   *.

All the listings in this guide will be shown with keywords in full, to make their meanings clear. By all means use minimum abbreviations - they speed up entering your program, as well as saving your fingers!

When you're using the command line (but not the task window), another technique that can mean less typing is using the cursor edit keys. If you are typing in a line which is similar to one already on the screen, you can use the arrow keys to allow you to copy part of what you already have. Suppose, for example, you were entering our earlier program:

   10PRINT "HELLO"
   20PRINT "GOODBYE"

After entering the first line, just type '20' then press the up-arrow key. The cursor will appear to move up one line, leaving behind a solid block. What has happened is that you now have two cursers. The block is in fact now the write cursor and the underline character the read cursor. Use the arrow keys to position the read cursor underneath the first of the characters you wish to copy, in this case the start of the word PRINT, and press the Copy key. The character above it will be copied to the position of the write cursor and both cursers will move one position to the right, allowing you to copy the next character. In this way, you can easily copy a chunk of your listing without having to type it in again.

you now have two cursers

... you now have two cursers

What are Variables?

The previous section contained a brief reference to variables. A variable is a number which is referred to by a name. We give it a value when we first create it, and we may change it during the program.

Enter Basic and try typing:

   x=3

We have created a variable called simply 'x' and given it a value of 3. Now try another one:

   y=x+2

Basic works out the value of the right-hand side of the equation, that is the part following the equals sign, and makes this the value of a new variable, called 'y'.

Because the value of x is 3, it doesn't take a mathematical genius to see that the value of y must be 5, i.e. 3+2. You can prove this by typing:

   PRINT y

Basic always works out the value of whatever follows the word PRINT before printing it, unless it is in quotes. For example:

   PRINT 2+3

will print:

   5

but:

   PRINT "2+3"

will print:

   2+3

This is why the words HELLO etc. which we printed in the previous section had to be in quotes. Without them, Basic would have thought that HELLO was the name of a variable and given you an error message saying 'Unknown or missing variable'.

Choosing Names for Variables

This is because a variable name doesn't have to be a single letter. In fact it is best if it's a word, chosen to describe what the variable does. Any group of letters and numbers can be used. Spaces are not allowed, but you can use an underline symbol (_) instead. The name must start with a letter and must not begin with a Basic keyword. You couldn't, for example, use TOTAL as a variable name, because 'TO' is a keyword. You can however, use 'total'.

This example shows the best way of avoiding the problem. All Basic keywords are in capitals. If you always put variable names in lower case, there will be no chance of one clashing with a keyword. Another advantage is that it makes the program a lot easier to read, as all keywords are in capitals and all variable names are lower case.

Types of Variable

There are three types of variable: floating point, integer and string.

Floating point variables are used to represent numbers that may contain fractions, for example:

   size=1.25

Note by the way that a full stop is used as a decimal point in Basic, as it is elsewhere on the computer.

Integer variables are used to represent whole numbers. An integer variable has a name ending in a '%' sign, for example:

   size%=20 

If you try to put a fraction into an integer variable, it will be rounded down to the nearest whole number.

There are three advantages of using integer variables:

  • They are handled faster
  • They use less memory for storage
  • They are completely accurate - a number that should be 10 will not end up as 9.99999999

It is always best to use integer variables if you can. Only use a floating point variable if its value is likely to contain a fraction, or be outside the limits -2147483648 to 2147483647, which integer variables can't handle.

Automatic Line Numbering

We'll look at string variables shortly, but first let's try a very simple program working with numbers. Enter Basic and type:

   AUTO

This will put the line numbers on the screen to save you having to type them at the beginning of each line. If you don't add any figures after AUTO, the line numbers will increase by 10 each time you press Return. Using this facility, type in the following:

   10PRINT "What is the first number?"
   20INPUT first%
   30PRINT "What is the second number?"
   40INPUT second%
   50PRINT first%+second%
   60GOTO 10

When you have finished, press Esc to get out of the AUTO facility. If you make a mistake and spot it before pressing Return, you can use the backspace key to delete back to it and try again. If you don't notice it until after pressing Return, you'll have to retype the line.

You can restart automatic line numbering at any point you like. If you had stopped, for example, after line 30, just type:

   AUTO 40

to continue. If you want the line numbers to increase in steps other than 10, for example 5, type:

   AUTO 40,5

Using INPUT

The first line of our program prints a message asking you for a number. Line 20 introduces the basic keyword INPUT. This makes the program wait while you type in a number, then makes it the value of the variable called first% when you press Return.

Lines 30 and 40 repeat the procedure so that you can enter a second number, making it the value of second%, then line 50 adds them together and prints the result.

The command GOTO in line 60 is all one word, without a space in the middle. It tells the program to jump back to line 10 and continue from there.

We've used the GOTO command here to avoid introducing too many new concepts in one go. It's not a good command to use, for reasons which we'll find out later, and this is the only place in this guide where it will be used.

When you run the program, it will ask you for a number, wait while you type it in, then ask you for the second one. When you have entered that one, it will print the sum of the two and ask you for a new first number - it has jumped back to the beginning and started again. The best way out of this program is to press Esc.

Errors Involving Variable Names

Lines 20 and 40 in the program we've just looked at create the two variables first% and second% by giving them values. They don't exist until these two lines are executed. Line 50 uses them but they have to exist already for this line to work. If for any reason one of them didn't, you would get an error message saying:

   Unknown or missing variable at line 50

Suppose you had made a mistake typing in line 20, so that it read:

   20INPUT frist%

You would still get the same error message saying that there was a mistake in line 50, although there would be nothing wrong with line 50. The trouble is that a variable has been created in line 20 as intended, but it's been called 'frist%' instead of 'first%' (Basic doesn't know you've made a spelling mistake!). When the program gets to line 50, it looks for a variable called 'first%' and, of course, it can't find it.

The number in an error message tells you the line where the error was detected, which is not necessarily where it occurred.

String Variables

The third type of variable is a string variable. This type doesn't represent a number, but a string of characters. Its name ends in a dollar ($) sign. An example would be:

   name$="Fred"

String variables can be concatenated or added together, which means that the strings of characters themselves are joined together. Try this:

   first_name$="Fred"
   last_name$="Smith"
   full_name$=first_name$+last_name$
   PRINT full_name$

You should get:

   FredSmith

Of course, a space between the first and last names would be nice. You could get one by modifying the third line to read:

   full_name$=first_name$+" "+last_name$

There is a space between the quotes.

Using Parts of Strings

As well as joining strings together, you can obtain parts of them, using the keywords LEFT$, MID$ and RIGHT$.

LEFT$ lets you take some characters from the left-hand end of the string. It is followed by the name of the string variable and the number of characters in brackets, for example:

   word$="ABCDE"
   PRINT LEFT$(word$,3)
   ABC

If you omit the number of characters, the result is the original string minus its last character:

   words$="ABCDE"
   PRINT LEFT$(word$)
   ABCD

RIGHT$ does the same thing at the right-hand end of the string:

   name$="ABCDE"
   PRINT RIGHT$(name$,3)
   CDE

In this case, if you omit the number of characters, you get the last character of the string:

   name$="ABCDE"
   PRINT RIGHT$(name$)
   E

MID$ allows you to take one or more characters from anywhere in the string. Like LEFT$ and RIGHT$, it is followed by the name of the string in brackets, but there are now two numbers, telling us the position in the string of the first character and the number of characters that we want, for example:

   name$="ABCDE"
   PRINT MID$(name$,2,3)
   BCD

The first of these two numbers is always needed but if you omit the second one, you get all the characters to the end of the string:

   name$="ABCDE"
   PRINT MID$(name$,2)
   BCDE

In all cases, you can use a variable in place of a number.

LEFT$, RIGHT$ and MID$ can also be used to replace part of a string, for example:

   LEFT$(a$,3)=b$

The first three characters of a$ are replaced by the first three characters of b$, or all of b$, if it is shorter than this. Similarly:

   RIGHT$(a$,3)=b$

replaces the last three characters with the first three of b$ and you can do a similar operation using MID$ (try it). None of these operations alters the length of a$.

You can find out the length of a string, that is the number of characters in it, by using the keyword LEN, for example:

   word$="ABCDE"
   PRINT LENword$
   5

Martyn Fox

previousmain indexnext

 
© Martyn & Christine Fox 2003