Back To Basics - Part 2

In the second part of his Basic programming series, Mark Moxon takes a look at the different types of variable and shows them in action

Right, so now you're able to type in programs and run them. Great. Now comes the tricky part: learning to create your own.

The first step in most tutorials is to look at the concept of a variable, and, just because this series is far cooler than most, there's no reason why we shouldn't start that way too. So, what's a variable?

The BBC Basic Guide says 'a variable has a name and a value associated with it'. This means we can invent a variable name for ourselves, and give it a value. So, if you want to define the number of eggs in a basket as seven, you create a variable called number_of_eggs with value 7 (variable names can't contain spaces, so we have to use underline). The command to do this is:

number_of_eggs=7

Now whenever we use the name number_of_eggs in our program, 7 will be substituted.

Why call them variables? It's because we can change the value associated with any variable at any time. If, later in our program, we have the line:

number_of_eggs=10

then the value for number_of_eggs will be 10 from that point on.

Try typing in Listing 1, which simply prints out two values when run:

Listing 1

REM >Listing1
:
ON ERROR REPORT:PRINT " at line ";ERL/10:END
number_of_eggs=7
PRINT "The value of number_of_eggs is ";number_of_eggs
number_of_eggs=10
PRINT "The value of number_of_eggs is ";number_of_eggs
END

In line 4 the variable number_of_eggs is set to 7, printed out in line 5 using the PRINT command, which will be described in full in a later article. In line 6 number_of_eggs is set to 10, which is printed out in line 7. Line 8 terminates the program, and line 3 is our standard error handler, described last month.

Variable types

There are three important types of variable: integer variables, which can only have whole number values; floating point variables, which can have any number values; and string variables, which have strings of characters as values.

You tell the computer which type you are defining by the last character you choose for the variable name. A name by itself, such as number_of_eggs, denotes a floating point; a name ending in a per cent (number_of_eggs%) denotes an integer; and a name ending in a dollar (number_of_eggs$) denotes a string. Don't worry about string variables: we'll look at them later.

In Listing 1 we used a name without a per cent character, so the variable number_of_eggs is a floating point variable. We could give the variable a non- integer value, such as 3.5, by altering one of the assignment statements (lines 4 and 6) to number_of_eggs=3.5 or similar. If we wanted to make sure the number of eggs was always a whole number (which makes sense) we could add a per cent to the variable name. This makes no difference in this example, but the following shows why you should make sure to use the correct variable.

The real power of variables is apparent when you consider expressions. An expression is the name given to something which can be evaluated to produce a value. The following are expressions: 1+2 and number_of_eggs+1, as they can be evaluated; the first to 3, and the second to the value of the variable number_of_eggs plus 1.

Variables can be assigned values not only by putting number_of_eggs=7 or whatever, but by putting any expression on the right-hand side of the equals sign. So the syntax for the assignment of a variable (in other words, the standard way of laying out the statement) is:

variable_name=expression

Try entering and running Listing 2:

Listing 2

REM >Listing2
:
ON ERROR REPORT:PRINT " at line ";ERL/10:END
number_of_eggs%=7
PRINT "The value of number_of_eggs% is ";number_of_eggs%
more_eggs%=number_of_eggs%+1
PRINT "The value of more_eggs% is ";more_eggs%
END

This makes number_of_eggs% seven, and defines more_eggs% to be the value of the expression number_of_eggs%+1. This evaluates to 8, No problems there, but try changing line 6 to:

more_eggs%=number_of_eggs%+1.5

The value of more_eggs% doesn't change, although we'd expect it to be 8.5. The reason is that more_eggs% is an integer variable, not allowed to contain a non-integer number. In this case, the value of 8.5 is rounded down to 8 when assigned to more_eggs%.

Now remove all the percentages, and run again. The expected value of 8.5 is shown. The moral is simple: only use integer variables when you need to, but do use them if you know a variable will never have a fractional value.

Expressions can be far more complicated than simple addition. Table 1 shows the more common numeric operators, though I have left out a lot of the more technical ones to keep things simple.

Table 1: Numeric Operators

Operator Function Example
+ Addition number_of_eggs+3
- Subtraction number_of_eggs-6
* Multiplication number_of_eggs*48
/ Division number_of_eggs/2
( ) Brackets (number_of_eggs+1)/10
^ Raise to the power number_of_eggs^2
DIV Integer Division number_of_eggs DIV 2
MOD Integer Remainder number_of_eggs MOD 2

Addition, subtraction, multiplication and division are all pretty obvious: the only thing to notice is that an asterisk is used as a multiplication sign. Brackets allow otherwise ambiguous expressions to be evaluated, such as 1+1/2, which could be evaluated to 1.5 or 1, depending on how you read it. Basic has a built-in method of avoiding this problem, 'operator precedence', but I reckon using brackets removes ambiguity and means you don't have to remember the rules to understand the expression. Stick to (1+1)/2 or 1+(1/2), rather than 1+1/2.

The raise to the power operator is also pretty easy: 5^2 means five-to-the-power-of-two, or 25. Finally, the integer operators. DIV is essentially the same as / except the result is rounded down to the nearest integer, and MOD gives the remainder from the division. 5 DIV 2 evaluates to 2 (as 5/2 is 2.5 which rounds down to 2), and 5 MOD 2 evaluates to 1 (as the remainder when 5 is divided by 2 is 1).

String variables

Before discussing variables and their uses further, we have to look at string variables, which have names like

number_of_eggs$
. A string is a string of characters, like "Abcdef" or "Welcome to our world!". Notice the string is in double quotation marks; this is how a string is denoted.

To assign a value to a string variable, you still use the same syntax as for the numerical variables, in other words:

variable_name=expression

This time the variable name ends in a dollar, so an example of string assignment would be:

name$="Mark Moxon"

Try entering Listing 3, a quick demonstration of assigning two string variables and printing them out one after the other.

Listing 3

REM >Listing3
:
ON ERROR REPORT:PRINT " at line ";ERL/10:END
first_string$="Hello. I am an"
second_string$="""Acorn"" Computer."
PRINT first_string$
PRINT second_string$
END

The strings are assigned in lines 4 and 5, and are printed out by the PRINT commands in lines 6 and 7. You can include a quotation mark in a string by putting two quotes next to each other; the quotes around the word "Acorn" are entered in the program using this method.

Variables

Basic has a large number of built-in functions for manipulating variables, these can be used in expressions to calculate various mathematical values. Table 2 shows a collection of mathematical functions which can be used on numerical (integer or floating point) variables. Table 3 shows some string functions. Using these, expressions such as (COS(angle)*PI)/ABS(sign)) can be constructed for your amusement.

Table 2: Numerical Functions

Operator Description
ABS(n) Makes n positive, so ABS(-65) evaluates to 65
INT(n) Rounds n down to an integer, so INT(6.7) evaluates to 6
SQR(n) Evaluates the square root of n, so SQR(9) evaluates to 3
COS(n) Evaluates the cosine of n (n is in radians)
SIN(n) Evaluates the sine of n 9n is in radians)
TAN(n) Evaluates the tangent of n (n is in radians)
ACS(n) Evaluates the arc-cosine of n (n is in radians)
ASN(n) Evaluates the arc-sine of n (n is in radians)
ATN(n) Evaluates the arc-tangent of n (n is in radians)

Table 3: String functions

Operator Description
LEN(s$) Evaluates to the length of the string s$ - the number of characters in the string
STR$(n) Converts the number n into a string, so STR$(456) evaluates to "456" (a string)

Listing 4 shows these in action, and should help explain how they work. Type it in and have a go.

Listing 4

REM >Listing4
:
ON ERROR REPORT:PRINT " at line ";ERL/10:END
n=6.57
s$="Acorn Computers"
PRINT "Value of n is ";n
PRINT "INT(n) = ";INT(n)
PRINT "SQR(n) = ";SQR(n)
PRINT "COS(n) = ";COS(n)
PRINT "SIN(n) = ";SIN(n)
PRINT "TAN(n) = ";TAN(n)
PRINT
PRINT "String s$ is """;s$;""""
PRINT "LEN(s$) = ";LEN(s$)
END

Next month I'll take a look at the functions available for variable manipulation in more detail, so don't worry if the tables don't instantly make sense: they will then.


Source: Acorn User - 134 - September 1993
Publication: Acorn User
Contributor: Mark Moxon