Copy Link
Add to Bookmark
Report
QuickBasicNews Issue 1: VARIABLES
…ÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕª
∫ VARIABLES ∫
»ÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕº
Although this publication is dedicated primarily to QuickBasic,
everything discussed in this column applies to programming in
QBasic.
We keep track of values within a computer by assigning names to
the values stored in the computer. For instance lets say we wanted
to keep track of the number 5280 ( the number of feet in a mile )
somewhere in the computer. We could assign certain memory locations
this value. In fact that is exactly what the computer does. But,
it could get awfully complicated for the programmer if he had to
keep track of exactly what values were located in exactly what
memory locations. By assigning names to the values we want to keep
track of, we can let the computer worry about where exactly in
memory the number is. Then, all we have to keep track of is the name.
In the above example we could add the following line of code to our
program
mile = 5280
[NOTE] Do not include a comma in the actual number. If you do you
will get a (Type mismatch) error when you run the program.
Now any time you want to use the value 5280 you can use the name mile
instead. With the above statement you have assigned the value of 5280
to the name ( mile ). You might also see the above statement this way
Let mile = 5280
Either statement is correct. Some earlier versions of Basic required
the use of the word Let. However, with QuickBasic the word Let is
optional. If the value for mile does not change during program
execution then mile would be considered what is called a constant
value. That is, a value that does not change. Constant value
assignments make it easy for you (or someone else) looking at your
code because it represents something that can easily be followed.
In general, mile above is a constant if you do not change its value
within the program. In strictly technical terms however, the
reserved word CONST assures that a names' value cannot change during
program execution. Use the keyword CONST before the values name
CONST mile = 5280
With this constant statement somewhere at the beginning of your
program the value of mile cannot be changed later in your program.
If you try to change its value you will get an error message.
(Duplicate definition). By using the CONST definition it will be
clear to anyone looking at your program later, that the value of
mile will always be 5280 anywhere throughout your program. Again,
you could have just said mile = 5280 and if you didn't change its
value during program execution then it is in effect a constant also.
However, using the CONST definition makes it absolute, and
will report an error message if you tried to change its value.
You can also define more than one CONST value on a statement line
by separating each constant name with a comma. For example
CONST mile = 5280
CONST months = 12
could be shown in two statements as shown, but could also be
defined this way:
CONST mile = 5280, months = 12
Even if you knew nothing about programming you would have some
idea about what the programmer meant when you saw a statement like
mile = 5280. Now, everywhere you wanted to use the number 5280
you could simply use the name mile instead. Later, if you wanted
to change the mile value to reflect yards instead of feet, you
could change the one statement that says (CONST mile = 5280) to
CONST mile = 1760.
By changing this one constant statement, you change the value
used throughout your program whenever it encounters the mile value.
Another assignment is made through the use of variable names.
A variable is a name assignment whose value may or may not change
during program execution. An example of a variable name might be
total = 150
If you were using the name total to keep track of changing values
due to some type of math calculations you would want to change the
number assigned to the name (total). The name (total) is then called
a variable name, because the value may change during program
execution. A variable name may or may not change during program
execution. The important thing to remember is that this value
assigned to the name (total) may change if you want it to.
With that out of the way there are a few rules regarding the naming
of variables that you must keep in mind. The first rule is that
you cannot use a name that is a BASIC reserved word. Basic reserved
words are the BASIC language command words. Examples of Reserved
words would include PRINT ( Because PRINT is a BASIC Command word )
and GOTO. Don't worry if you don't know all the BASIC reserved
words. If you try to use one, you will get an error message. Variable
names are also limited to 40 characters maximum and 1 character minimum.
A variable name must begin with a letter of the alphabet. Examples of
valid variable names would include
receipts = 10
themonthoffebruaryhasthismanyday = 28
x = 20
K1= 50
v = y Yes this is valid. This is because y equals some
number. If a program statement prior to this
assignment was say y = 10 then when this
statement executes then v = 10. If no previous
assignment was made for y, then y will equal a
zero.
Examples of invalid variable names would include
print = 10 Invalid because print is a reserved Basic word
gosub = 9 Invalid for same reason
1able = 20 Invalid because name must start with a letter
Although you often want to assign numbers to names there are other
times when you want to assign strings of characters to a name. A string
of characters may include alphanumeric characters, that is, numbers or
characters from the alphabet. At these times you will be using what is
called a string variable. The rules for naming string variables are
similar to the rules for simple variables as talked about previously.
However, the main difference in distinguishing a string variable from
a normal variable is the addition of a dollar sign ($) at the end of
the variable name. The characters that we want assigned to the
string variable are then placed within quotation marks. Examples
of string variables
myname$ = "David"
february$ = "28"
With this in mind the following name assignments would be invalid
and would result in the error message (Type mismatch).
myname = "David" Invalid due to lack of $
february$ = 28 Invalid because 28 is not within quotation marks
Since we can assign the value of 28 to the name February with the
statement
February = 28
You might well ask what is the difference in making a string
assignment of February$ = "28" ?
The answer is, there is a great deal of difference. February = 28
assigns a number to the name February, whereas february$ = "28"
assigns a string of characters to the string variable. Mathematical
computations can be carried out on the variable called February.
Match computations cannot be carried out on the string variable
we have called february$. String variables can be manipulated
in ways that simple variables cannot. For the purposes of this
months' discussion, it is only important that you understand the
difference between the two.
The following is an example of some code that might do a math
computation using variables that have been assigned values. [NOTE]:
When you see an apostrophe ( ' ) after a program statement, everything
to the right of the apostrophe on that line is simply a remark
statement and will be ignored when the program is executed.
mile = 5280 ' assign the value 5280 to the name mile
altitude = 15840 ' assign the value 15840 to the name altitude
mileshigh = altitude/mile 'here the program will compute the items
' to the right of the equal sign and then
' assign that value to the name
' mileshigh. After the computation the
' names mileshigh will equal the number 3
You could just as well have assigned the name for the value 5280 a
name such as (jimminy). What you name it is unimportant to how it is
handled within the program. However, naming the number of feet in
a mile to the name (mile) makes much more sense than assigning the
number 5280 to a name of (jimminy). By making your name assignments
meaningful, it becomes much easier to follow your program logic. This
will become very clear to you as your programs become longer and
more complex. It will also make it easier should someone other than
yourself try to follow your code. You can't always make your names
meaningful. However, whenever possible make your names meaningful.
It's often very hard for even the original programmer of a piece of
code to try to follow his own code at a later date if he doesn't
make meaningful assignments.