Classic Computer Magazine Archive A.N.A.L.O.G. ISSUE 72 / MAY 1989 / PAGE 58

BASIC TRAINING
by Clayton Walnum

    Programming a computer can be a rewarding hobby. It puts into your hands the ability to make your machine do what you want. Further, programming is a good mental exercise; getting a program to run properly is a lot like trying to complete the Sunday crossword puzzle-with the difference that you have something to show for your efforts, something you can use.
    And, contrary to what you may have heard, programming a computer is not that difficult. It can be difficult. It all depends upon the level of expertise you wish to obtain. Simple programs can be written after only an hour or two of study. Of course, programming a fast-action arcade game may not be possible without years of programming experience.
    For the most part, though, you can quickly develop the skills required to write useful programs. This column is dedicated to getting those of you with no programming experience up to speed as fast as possible. We'll cover all the topics necessary to make you into a proficient BASIC programmer, starting at the very beginning and working our way through to the more difficult topics only when you are ready for them.

What Is a Program?
    To write a program we first have to understand what it is. Those of you who have just purchased your first computer (and even some of you who have owned a computer for a while) may have a lot of misconceptions about what a program is. You probably think of a program as sheet after sheet of high-tech hieroglyphics that can make sense only to "techies. " The fact is that a program is nothing more than a list of instructions. These instructions are placed in the order necessary to accomplish a particular task. A program, in fact, is not unlike a recipe.
    Think about the steps you must complete to bake a cake. Now imagine that you must tell someone else how to bake that cake. You might tell them to go to the pantry and get out the cake mix, place the cake mix into the bowl with the other ingredients, mix the ingredients, pour the batter into a pan, then place the pan in the oven which has been preheated to the correct temperature.
    A computer program is much the same, but instead of telling someone how to bake a cake, you're telling the machine how to perform whatever task it is that you want it to do. For example, let's say that we want the computer to take two numbers from the user, total them and then print the results. The steps we need to accomplish might look like this:

STEP 1: CLEAR THE SCREEN
STEP 2: ASK FOR THE FIRST NUMBER
STEP 3: GET THE FIRST NUMBER
STEP 4: ASK FOR THE SECOND NUMBER
STEP 5: GET THE SECOND NUMBER
STEP 6: ADD THE NUMBERS
STEP 7: PRINT THE TOTAL

    This all looks very logical, right? There's nothing "high-tech" about any of the above instructions. All we have to do to convert the preceding list of instructions into a computer program is to write them in a way that the computer can understand. For example, we can't tell the computer "Clear the screen" because it doesn't understand our language. We need to use a language that it does understand, and one of those languages is BASIC. In BASIC, one of the instructions to clear the screen is "GRAPHICS 0." Here is the list of instructions translated into BASIC:

10 GRAPHICS 0
20 PRINT "ENTER THE FIRST NUMBER"
30 INPUT NUMBER1
40 PRINT "ENTER THE SECOND NUMBER"
50 INPUT NUMBER2
60 TOTAL=NUMBER1+NUMBER2
70 PRINT TOTAL

    If you type the above program into your computer (go ahead and do it, if you like) and then run it, you would get the following on your screen (items typed by the user are shown in italics):

ENTER THE FIRST NUMBER
?12
ENTER THE SECOND NUMBER
?13
25

    As you can see, a computer program is not very different from any other set of instructions. In fact, even without knowing BASIC, you can generally see what is going on in the program. It's just a matter of translating each step into the language the computer can understand. Of course, in order to do that translation, you have to have a "dictionary" of words that the computer knows. Each of the words in the BASIC language is called a "keyword" or a "reserved word," and a list of them is shown in Table 1. Take a look at this list, but don't worry if you can't figure out what some of the words mean. We'll get to them all sooner or later.

ABS
ADR   
AND
ASC
ATN
 BYE
CLOAD
CHR$
CLOG
CLOSE
CLR
COLOR
COM
CONT
COS
CSAVE
DATA
DEG
DIM
DOS
DRAWTO
END
ENTER
EXP
FOR
FRE
GET
GOSUB
GOTO
GRAPHICS
IF
INPUT
INT
LEN
LET
LIST
LOAD
LOCATE
LOG
LPRINT
NEW
NEXT
NOT
NOTE
ON
OPEN
OR
PADDLE
PEEK   
PLOT
POINT
POKE
POP
POSITION
PRINT
PTRIG
PUT
RAD
READ
REM
RESTORE
RETURN
RND
RUN
SAVE
SETCOLOR
SGN
SIN
SOUND
SQR
STATUS
STEP
STICK
STRIG
STOP
STR$
THEN
TO
TRAP
USR
VAL
XIO

Table 1.

    The list of words in Table 1 may seem large, but consider that they're all you need to do practically anything you want with your computer. That's pretty amazing, don't you think?

Three Modes
    If you own an XL or XE computer, the BASIC language is built into your machine. To access BASIC, all you have to do is turn on the machine. You should then see a blue screen with the word "READY" displayed. READY means that BASIC is standing by, ready to process your next instruction. The only way to get rid of BASIC is to hold down the Option key when turning on the computer.
    If you own an older model of Atari-a 400 or 800-BASIC comes on a cartridge. To access BASIC, you must plug the cartridge into the machine before you turn it on.
    The BASIC language operates in three modes: immediate, deferred and execution. You are using the immediate mode whenever you type an instruction that isn't prefaced with a line number. For example, type the following line and press Return:

    PRINT "HI THERE!"

    As soon as you pressed Return, the computer performed your instruction, printing the words "HI THERE!" on the screen. You are in the deferred mode whenever you type instructions that begin with a line number. The number tells BASIC that the instruction is not to be performed right away, but is instead to be stored in the computer's memory for later use. Type in the line above again, but this time put a line number in front of it like this:

    10 PRINT "HI THERE!"

    When you press Return this time, nothing happens on the screen. The computer has stored the line in memory and is waiting for you to tell it when you want it performed.
    Whenever you type the BASIC keyword "RUN," the machine enters the execution mode; that is, it begins to perform whatever instructions you may have previously stored in the machine's memory. Type RUN now and see what happens. The words "HI THERE!" should appear on your screen as the computer performs the instruction you entered previously in the deferred mode.

Line Numbers
    What's all this talk about line numbers? Remember that a program is a series of instructions that must be performed in a certain order. The computer knows the general order of the instructions by the line numbers included with each instruction. That's not to say that every BASIC program is performed starting with the first line and working its way one line at a time to the last line. This isn't true because there are ways to change the order in which the instructions are executed. But the line numbers do organize the way the program will work in a general way. One thing is for sure: the first line of your program will always be the first line executed.
    Line numbers can be any number from 0 to 32727. As you write your program, though, you'll want to leave "space" between each line number so that you can insert lines between existing ones. For example, take a look at this program:


0 GRAPHICS 0
1 PRINT "ENTER FIRST NUMBER"
2 PRINT "ENTER SECOND NUMBER"
3 INPUT NUMBER2
4 TOTAL=NUMBER1+NUMBER2
5 PRINT TOTAL


    Do you see something wrong here? If you compare this program to the similar one above, you'll see that we forgot the line "INPUT NUMBER1." Because that line is missing, the program won't run properly. How can we fix it? Outside of manually changing the numbers for each line, we can't. But suppose we had written the program without using consecutive numbers, like this:

0 GRAPHICS 0
10 PRINT "ENTER FIRST NUMBER"
20 PRINT "ENTER SECOND NUMBER"
30 INPUT NUMBER2
40 TOTAL= NUMBER1+NUMBER2
50 PRINT TOTAL

    To fix the program, now all we have to do is type the following line:

    15 INPUT NUMBER1

Because of the line numbers, the computer knows exactly where this line belongs in the program, and it'll put it there for us, giving us this program:

0 GRAPHICS 0
10 PRINT "ENTER FIRST NUMBER"
15 INPUT NUMBER1
20 PRINT "ENTER SECOND NUMBER"
30 INPUT NUMBER2
40 TOTAL =NUMBER1+NUMBER2
50 PRINT TOTAL

    You can see now how important it is to number a program in such a way as to allow changes later on. Almost all the programs you'll write will require some changes and polishing to get them to do exactly what you want.

Constants and Variables
    Before we can understand exactly what's going on in our program, we have to learn about two types of data: constants and variables. A constant is a piece of program data that never changes. An example from our program is the 0 that follows the command GRAPHICS and the words "ENTER FIRST NUMBER" that follow our first PRINT statement. The former is a "numerical" constant, and the latter is a "string" constant. (A string is defined as a series of characters.) Nothing we can do in the program can change these constants; the only way to change them is to edit the program itself.
    A variable is a piece of data that can be changed within our program. If you've taken algebra, you've already had some experience with variables. Basically (no pun intended), they are holding places for data that are identified by a name. For example, in line 15 of our example program we have a variable named NUMBER1. You can think of NUMBER1 as a box with enough space to hold a number-any number at all. In this case, NUMBER1 will hold whatever number the user types in (you'll see why later). We don't know in advance what that number might be, and we don't care. NUMBER1 is a numerical variable. There are string variables too, but we won't talk about them just yet.

What's Going On?
    Now that we know a little about how BASIC works, let's take a look at the program we just wrote and see what it is doing.
    Line 0 in the program tells the computer to go into graphics mode 0. The Atari computers have many different graphics modes, each with its own number, but we won't talk about many of them for a while yet. It's enough to know that Graphics 0 is Atari's standard "text" mode; that is, the mode that is used strictly for printing words or numbers on the screen, rather than drawings. When you first turn on your machine, it automatically comes up in graphics mode 0; in fact, when we ran the above program, we were already in graphics 0. So why did we have to instruct the computer to go to that mode again? Actually, we didn't. The program would have run fine without that line, with the exception that the screen would not have cleared first. That's one of the side effects of the GRAPHICS command. It always clears the screen.
    Line 10 in the program prints our first "prompt." When programming you have to remember to tell your program's user what you expect him to do. Our program would work fine without printing the prompts. But without the prompts, all we'd see on the screen would be the question marks (refer back to the figures in the "What is a program?" section to see the question marks I'm referring to); we'd have to know ahead of time what the computer was waiting for.
    The PRINT command is a very sophisticated instruction, one that allows many variations. The form shown in this line is the simplest one. The text within the quotation marks is displayed on the screen, and the cursor is moved to the next line. (The cursor always marks the place where the next PRINT statement will print.)
    Line 15 uses the INPUT command to tell the computer to get some information from the keyboard. It prints a question mark, then waits for the user to type something. Whatever number the user types will be stored in the variable that follows the word "INPUT." In our case, the variable is the numerical variable NUMBER1. (If you type something other than a number, you'll get an error. Numerical constants can only hold numbers, nothing else.)
    Line 20 prints our next prompt.
    Line 30 does the same thing as line 15, but this time the number that is typed is stored in the numerical variable NUMBER2.
    Line 40 takes the two numbers stored in NUMBER1 and NUMBER2 and places their sum in the numerical variable TOTAL. You'll notice that there are no BASIC keywords, or commands, here. This line is an example of a mathematical expression. In BASIC, an expression is calculated working from right to left. In other words, first NUMBER1 and NUMBER2 are added together; then the result is placed in the variable TOTAL. Expressions like this can be confusing to those who are familiar with algebra because the equals sign in this case doesn't mean "equals"; rather it should be interpreted as "gets." In English, line 40 would read "the numerical variable TOTAL gets the sum of the numerical variables NUMBER1 and NUMBER2." This type of expression is called an "assignment" statement because we are assigning a value to a variable.
    There are five arithmetic operators you can use. They are:

+ addition
- subtraction
x multiplication
/ division
^ exponentiation

    Line 50 uses our handy print statement to display the value of TOTAL on the screen. Notice that this time we haven't used any quotation marks after the word "PRINT." Leaving out the quotation marks tells the computer that the word after the PRINT is not a string constant, but rather a variable whose value we want to know. The number stored in TOTAL is printed on the screen. Confused? Let's say that the user typed in 5 and 35 as NUMBER1 and NUMBER2. TOTAL would then be given the value 40. Contrast the following examples of the PRINT statement:

PRINT "TOTAL"← You type this
TOTAL ←---------- You get this
PRINT TOTAL ←- You type this
40 ←---------------- You get this

    See the difference the quotation marks make?

Wrap Up
    That's enough information for you to ponder this month. Study what you've learned, and next month we'll continue our exploration of BASIC programming.