Thursday, 28 January 2010

Numeric Variables

This lesson will use  the INPUT Command and the CLS Command. It will also expand the use of variables seen coincidently in the FOR NEXT loops lesson.

A computer would be a rather boring thing if it weren't for input. Without user input a program would just run and then give some kind of answer or perform some kind of trick and that would be that. So if we wrote the line PRINT "4+4"  and then ran the program it would display 4+4.  However if the line was PRINT 4+4 and we ran the program the computer would first work out the answer 4+4 is 8 and display the answer. As you can see the difference in the lines are the use of quotation marks. Anything after a PRINT statement within quotation marks will be displayed 'as is' but if the quote marks are absent, and what follows is a calculation of some sort, the computer will calculate the answer before displaying it. At one time in the early days of computing, before the invention of the user keyboard this was the only way that calculations could be carried out so each time a calculation of a different amount had to be computed the program had to be stopped and changed accordingly and then rerun. (*See Also)
However the computer scientists at Dartmouth college where the BASIC language was developed invented a procedure to allow users to interact with programs directly and one of these ways was the use of the INPUT command and the variable. In basic and all other programming languages the numeric  variable is a the most important devices that allow numbers to be assigned to a letter or words much like the way that formula are expressed. The Basic language was developed for scientists who wanted some simple way to interact with computers so what better way than to structure the language in a way that was already familiar to them.  So if I were to write the statement PRINT Temperature, when run the program would return the answer 0. The reason it did this was that as the word Temperature was not contained within quotation marks Basic assumed that the word Temperature was an 'on the fly' numeric   variable and as we had not assigned a value to the variable 'Temperature' it assigned the value of zero itself. This in fact is one of the reasons why computer scientist didn't like the Basic language because of the confusion that can be caused by  not declaring variables either at the beginning of a program or within local procedures (we will get on to declarations and local procedures, global and local variables later in the course).

So now open up the editor and type in the following code
___________________________________________________

PRINT Temperature

__________________________________________________

Now run the program

Now alter the program to read

____________________________________________________

LET Temperature = 10


PRINT Temperature
___________________________________________________

What we have done here is to assign a value to the numeric variable 'Temperature' before the program begins and this is then displayed.

Now alter the code again and this time it should read
___________________________________________________

LET Temperature = 10


LET Change = 2 


PRINT Temperature + Change

________________________________________________

This time the computer has added both the variables together and displayed the answer 12.

This still means that every time we need to change any of the variables the program has to be altered and rerun. 

So now lets use an INPUT statement to make the program a little more user friendly. Alter the code again to read.

 Note: As I have already said, in Basic you do not have to declare the variables but it's good practice and makes the program more readable for other people. Also calling variables by a name that makes their use clear rather than just one letter such as 'a' or 'b' also helps in this regard. 

_____________________________________________________

REM Declare and initialise the Variables


DIM  Temperature AS INTEGER 
DIM   Change  AS INTEGER


REM Clear the screen

CLS


REM Get data from the user
INPUT "Please enter the temperature" ;  Temperature
INPUT "Please enter the change in temperature" ; Change



REM Print the calculated results
PRINT "The new Temperature is " ; Temperature + Change
_______________________________________________________

Note the introduction of the CLS command which clears the screen before the program is run

Notice that the quoted text after each of the INPUT statements and before the Variables is displayed as a prompt for the user.

Note the use of the  '; ' After the prompt, you do not have to use this if you are using the INPUT statement without a user prompt.

Note the use of REMarks throughout the program  to help you or any other reader understand the program.

Revision:

Numeric Variables are letters 'a' 'b'  'c' or words 'Temperature' 'change ' that are used within programs to hold variable numeric data.

Printing "4+4" is different to printing 4+4

INPUT statements allow users to assign values to numeric variables.

Variable names can be added together and printed, such as in PRINT Temperature + Change

No comments:

Post a Comment