Tom R. Halfhill, Editor
Forget
Your Algebra
Don't be misled into thinking that an extensive math background is necessary to program computers. Sometimes, it turns out, too much math knowledge confuses things when you're learning to program.
For instance, the following statement is perfectly acceptable in BASIC, but utter nonsense in math ematics: X = X + 1. It would probably earn you extra homework in a beginning algebra class because one of the first things they teach you is that one side of an equation must equal the other.
But in BASIC, not only is X=X +1 valid, so is X=X+2 or even X = X + 10000. Part of the difference is in the way that algebra and BASIC handle the symbol X, called a variable. In algebra, a variable is an unknown value; it represents a number you're trying to discover by solving the equation. In BASIC, a variable is a method of storing a value that can change as the program runs. Ordinary numbers are known as constants, because numbers don't change. In the statement X = X + 1, the number 1 is a constant, and 1 is always 1.
A variable, on the other hand, is like a flexible number. It can equal anything. And you can change what it equals anywhere in the program. The statement X = 5, called an assignment statement, sets the variable X equal to 5. (Actually, X = 5 is an abbreviation for LET X = 5. But the keyword LET is optional in almost all modern versions of BASIC, so it's rarely used anymore.)
After a variable has been assigned the value of 5, the computer treats it like a 5 anytime it subsequently encounters that variable when running the program. The advantage of using a variable instead of a constant to represent 5 is that the variable can be manipulated in a number of ways. Try running this simple program:
10 X=5:PRINT X:X=X+1:PRINT X
When it's done, you should see the numbers 5 and 6 on the screen, even though the program starts by setting X equal to 5. Why? Because the third statement- X = X + 1- is another assignment statement which adds 1 to the current value of X. Since the current value happens to be 5, then 5 plus 1 equals 6. The final statement prints the new value.
Run the program again after removing the first statement. You'll probably see a 0 and 1 on the screen. That's because almost all personal computers automatically initialize variables to zero when the program starts. Be aware, however, that some larger computers don't do this. Instead, the variable may contain an unknown, or garbage, value. To keep these garbage values from messing up calculations, programs written for these computers usually begin by initializing all variables to zero.
Variable Names
You're not limited to the letter X as a variable name, of course. You can use any letter from A to Z. Longer names are possible, too, and help make your programs easier for others (and even yourself) to understand. For instance, if you need a variable to hold the sum of a series of numbers added together, SUM is more readable than S.
Different versions of BASIC have different rules for variable names. In Commodore and Apple soft BASIC, variables can consist of letters and numbers but no symbols, as long as the first character is a letter. A1 is allowed, but not 1A. Commodore and Apple variables can be of any length, but only the first two characters are significant. That means the computer looks only at the first two characters of the name to decide if it's unique. SUM and SAM are treated as different variables, but SUM1 and SUM2 are not. Watch out for this, because it can lead to mysterious programming bugs.
Also, Commodore and Applesoft BASIC (and most other versions of BASIC) don't allow variables with reserved words. That is, any word that BASIC recognizes as a command, statement, or function cannot be part of a variable name. This restriction, too, can lead to mysterious errors. An example is the variable TOTAL. It looks as innocent as SUM, but contains the keyword TO (which is part of the FOR/NEXT loop statement, as in FOR X = 1 TO 10).
IBM BASIC permits variables with letters, numbers, and decimal points, as long as the name starts with a letter. Names can be of any length, and the first 40 characters are significant. Although a variable cannot be a reserved word, it can contain a reserved word. Therefore, the variable TOTAL is okay but the variable TO is not.
In Atari BASIC, variables may contain letters and numbers, as long as they start with a letter, and can be of any length with all characters significant. What's more, variables can include reserved words or even consist of a reserved word if the assignment statements use the optional keyword LET. Thus you can have a statement such as LET LET = LET + LET. In TI BASIC, variables are limited to 15 characters (all significant) and can start with either a letter or one of the following symbols: @ , [, ], /, and _. Oddly, though, the rest of the name cannot contain a [, ], or /.
Up to now we've been discussing numeric variables-variables that represent ordinary numbers. Next month we'll examine other types of variables.