8/VARIABLES AND CONSTANTS
Earlier we talked about the two types of data Micro Color BASIC recognizes-Numeric and String Data. You can assign both types of data to variables, creating Numeric Variables and String Variables.
Numeric Variables
Throughout this manual, we've referred to the term "numeric expression". A numeric expression is either a number or a "variable" (such as a letter) which has been assigned a numeric value. Once you assign a number to a variable, the MC-10 will remember that number until you erase memory, turn the Computer off, or assign another number to the same variable.
As an example, type this:
A = 5 [ENTER]
Try to confuse the MC-10 by typing anything you want and pressing [ENTER]. To see if the Computer remembers the value of A, type:
PRINT A [ENTER]
Your TV will look like this:
PRINT A
5
In this example, A is the variable (because the value can change-it varies) and 5 is the constant (because its value will not change-it's constant).
What Kind of Variables Does the MC-10 Recognize?
In the preceding example, A is a "numeric variable" because it represents a number. You can use any two-character combination as a numeric variable as long as the first character is a letter; the second character can be either a letter or a number.
The only exceptions are letter combinations which are Micro Color BASIC commands, or statements (called "reserved words") such as IF, OR, NOT, READ, etc.
Micro Color BASIC does not permit variable names to contain embedded reserved words. This means that you cannot use the word NOTE, for instance, since the MC-10 will see the word NOT, which is a reserved word. However, reserved words can contain spaces.
Note that you can use an entire word as a variable. Micro Color BASIC, however, recognizes only the first two letters. This means you can use the word VARIABLE as a variable, although the Computer really only looks at the first two letters.
A1 |
1A |
AA |
10 |
VARIABLE |
NOTE |
NUMBER |
1 |
Z |
PRINTER |
Acceptable Numeric Variables |
Unacceptable Numeric Variables |
How Can You Use Numeric Variables Effectively?
The MC-10 allows variables to be used in a variety of ways. For a quick example, try this:
A = 5 [ENTER]
B = 10 [ENTER]
PRINT A + B [ENTER]
Your TV will display:
15
OK
You can perform any of the arithmetic function (subtraction, multiplication, division, and addition) using numeric variables.
String Variables
In the first part of this chapter, we mentioned that a string is any information enclosed in quotation marks. When you PRINT that information, it will be displayed exactly as it appears inside the quotes.
You can assign string data to variables in almost the same manner that you can assign numeric data to variables. The only difference is that string variables must end with a dollar sign ($). For instance:
A$ = "STRING VARIABLES"
When you type in the command:
PRINT A$ [ENTER]
the TV will display:
STRING VARIABLES
OK
The same rules that apply to numeric variables, apply to string variables except that string variables must be suffixed by $.
Adding String Variables
String variables can be merged by "adding" them together. (In technical terms, this is "concatenation".)
Type NEW [ENTER] (to erase memory). Then type in this program as an example:
10 A$ = "MICRO "
20 B$ = "COLOR "
30 C$ = "BASIC"
40 PRINT A$ + B$ + C$
It's important to note the blank spaces after MICRO and COLOR. They must be included within the quotes if you want spaces between the words.
When you RUN the program, your TV will display:
MICRO COLOR BASIC
If you try to "subtract", "multiply", or "divide" string variables, Micro Color BASIC will return a Type Mismatch Error (?TM ERROR).
The LET Command
When you assign variables, you have the option of using the LET command in the following manner:
LET variable=constant variable is either a string or numeric variable and constant is the value assigned to variable. |
To use LET, simply place it before the variable (string or numeric) and assign the value as normal. For instance:
LET A = 5
is the same as
A = 5
For string variables:
LET A$ = "STRING VARIABLE"
is the same as:
A$ = "STRING VARIABLE"
LET can make programs easier to follow, but remember that it takes up memory space and is optional.
|
|