In the early days of computing values and programs were entered into the computer using physical on/off switches. This was a very laborious task as you can see from the film on the Manchester University Baby and the American ENIAC machine. Originally some computers were analogue which meant that values were represented by varying voltages but in time it was found that although slower, digital was a better solution. At first some computing machines used decimal notation but again binary was found to be more efficient.
As previously said data was stored in the memory as a series of 0 and 1 so on an 8 bit machine like most of the early home computers, integer (whole numbers) numbers were stored in a series of bytes. 1 byte = 8 bits or 8 switches and 1 byte can store an integer number up to 255. Storing larger integer numbers requires multiple bytes and storing decimal numbers is even more complex.
As computers developed, assembly languages were written to allow program instructions to be entered via more understandable instructions. Assembly language had a 1 to 1 relationship with CPU (Central Processing Unit) instructions so LD A 10, once assembled would instruct the CPU to load the value 10 into a register called the accumulator. Assembly language simplified program construction and debugging but only to a point and left the memory assignment of programs and data up to the programmer. This meant that it was perfectly possible for programs to be inadvertently overwritten leading to program corruption. In time higher level languages were developed that combined a number of lower level instructions to be carried out by the use of series of more understandable syntax. This assisted non scientists because it meant that they could use computers as tools without having to understand how the computer worked, much like we may drive cars without understanding the mechanics.
BASIC stands for Beginners All Purpose Symbolic Instruction Code and was developed in Dartmouth College New Hampshire in the USA in 1964. Basic dealt with the storage of strings (words and numbers in character format) and numbers (as numeric values) in the form of variables. Variables allow the programmer to input and store and perform arithmetic tasks numbers during a programs execution without having to reserve memory or in fact understand what or how the computer was achieving this.Even better, names could be assigned to variables that would describe their purpose which made reading and debugging much easier. QBasic uses symbols to denote the type of variable and the type of data being stored.
Variable identifiers
$ String
% Integer
& Long
! Single
# Double
So A$ (A being any letter or word and the dollar sign assigns the type) denotes a string of characters which could be a name or even a string of numbers. Regardless of what characters are typed into a string they would be regarded as characters even if they are number characters.
A numeric variable can be a single letter or a word so TEMPERATURE% or T% would be a integer variable. One of the objections to the Basic language is that it allows programmers to use variables on the fly without declaration so if the programmer ran the following line
PRINT TEMPERATURE
the program would display the result 0 Not the word TEMPERATURE. Other Languages such as Pascal are more structured (Stricter) and require variables to be declared before they are used. In QBasic variables can be declared using a Dim statement ( Dim Stands for Dimension) but can also be used without prior declaration. The following code explains:
LET Value = 10
PRINT Valeu
When the program is run the result will be 0 and not 10. As you can see the variable in the PRINT statement has been miss spelt and therefore Basic assumes that Valeu is is a new variable and prints 0 to the screen and not the value assigned the variable Value. In this case it is easy to debug the program but in large programs it is very easy to make a mistake that may take a long time to track down.
Many well structured programming languages also require a declaration of variable types so although not necessary in QBasic it is good practice to do so.
DIM FirstName as STRING
DIM LastName as STRING
REM Declare the Variables FirstName and LastName as a string Variables
DIM Age as INTEGER
REM Declare Age and integer
REM When used the program the identifier can be added to the end
REM of the variable name so you could add the lines
INPUT "Enter First Name"; FirstName$
INPUT "Last Name"; LastName$
INPUT "Age"; Age%
PRINT "Name"; FirstName$; " ";LastName$; " Age "; Age
So in reality nothing stands in the way of the QBasic programmer writing relativity structured code and I would advise anybody learning QBasic to do this from the beginning. This will make migration to other programming languages much easier and will make your QBasic programs more readable and easier to control.
No comments:
Post a Comment