` ANTIC VOL. 5, NO. 1 / MAY 1986`

# NEW OWNERS COLUMN

## Lesson 3: Variables, IF/THEN

by DAVID PLOTKIN

In the past two issues of Antic, we introduced programming on your 8-bit Atari computer in BASIC and covered some simple instructions to get you going. This month we'll start doing useful work and learn some important new progmmming commands, culminating in a type-in, computerized "Hangman" game.

VARIABLES

Before proceeding, it is important to understand the concept of a variable. Variables can be assigned different values during the running of a program. For example, you may see a statement like the following:

10 LET XXX=10.5

This statement assigns the value of 10.5 to the variable named XXX. LET is a BASIC command for assigning values to variables. It is optional, however, and may be left out as shown below

10 XXX = 10.5

This second version is also fine. XXX may very well be assigned a different value elsewhere in the program, and after this happens, it will not continue to be equal to 10.5. Instead, it will now equal whatever new value has been assigned to it. As an example, try the following short program:

10 XXX=0:REM set variable XXX equal to the value of 0.
20 XXX = XXX + 1:PRINT XXX:REM change the value assigned to XXX.
30 IF XXX<10 THEN GOTO 20 Look at line 20. Something new is going on here. Line 20 is saying "variable XXX is being assigned the value equal to the current value of XXX plus 1." Thus, XXX will be equal to 1, then 2, then 3 and so forth. This illustrates two important principles about variables. The first is that a variable may be assigned a value which is calculated by the arithmetic operators. (These operators will be discussed in a future column.) Line 20 is quite simple, but the calculation can be as complex as you'd like:

20 XXX = 4*(PP-2)/3 * 24

The above example is perfectly valid. A second principle is that other variables can be included in the equation, including the variable whose value is being reassigned. In BASIC, references to the variable which appear to the right of the equal sign refer to the old value of the variable. As an example:

10 XXX = 5:XXX = XXX + XXX:REM now XXX is equal to 10, 5+5.
20 XXX = XXX + XXX:REM now XXX is equal to 20, 10+10.

Variable NAMES, like XXX in the above example, may be as long and descriptive as you desire. However, you'll want to use a variable name that gives a hint of the variables's purpose. For example, in the listing accompanying this column, you'll see variables such as incorrect and correct. It isn't too hard to figure out which one records the number of correct letters in your word!

You should not use BASIC commands for variable names (RUN, for example, is not a good variable name). Nor can you use variable names whose first letters correspond to a BASIC command. This is not as hard to do as you might think--variable names such as FORM-LENGTH won't work because the first three letters are FOR, a BASIC command. Variable names must also start with a capital letter, and contain only letters and numbers. Except for these restrictions, however, variable names may be just about anything you like.

IF/THEN DECISIONS

During the course of a program you will frequently need to execute certain commands based on the specific conditions existing during that particular time. For example, you might want to execute one set of lines if a variable is equal to one value, but execute a different set of lines if the variable equals something else.

Atari BASIC has a powerful pair of commands which test for certain conditions and then execute the appropriate program statements based on the results of these tests. The two commands, IF and THEN, must be used together. This month's listing contains several examples using IF and THEN.

The IF/THEN command consists of two parts. The first part is called the test and the second part is called the decision . The test occurs right after the IF statement. The decision occurs right after the THEN statement:

10 IF (Test) THEN (Decision)

The test, logically enough, determines whether certain conditions have been met. The test can be as simple as whether two variables are equal:

IF XXX=YYY THEN. . .

The test can also be quite complex and involve calculations :

IF (XXX * 2 + 3/4) =YYY/44 THEN. . .

Note that the * is the symbol for multiplication. All arithmetic and algebraic notation will be discussed in a future article.

The test can also determine whether several different conditions have been met. The keywords AND and OR will be discussed in a future column, but their use should be fairly intuitive. To test whether several different conditions are all true, use AND: IF (XXX=4) AND (YYY=8) AND (ZZZ=2*YYY) THEN . . .

To test whether one of several conditions is true, use OR:

IF (XXX=4) OR (YYY=8) THEN...

You may also combine them:

IF (XXX = 4 AND YYY= 8) OR (ZZZ = 10) THEN. . .

This statement will evaluate as true if both XXX = 4 and YYY= 8, or if ZZZ = 10. If all three conditions are true, then the statement will also evaluate as true.

Once you have determined whether a condition is true, you must tell the program what to do about it. This is the decision. In the following example, the program will print "TRUE" if XXX = 4:

IF XXX=4 THEN PRINT "TRUE"

In fact, a whole series of statements can be executed after the THEN command:

10 IF XXX + 4 THEN PRINT "TRUE":PRINT "XXX = 4":GOTO 10

It is important to remember that following THEN will be executed if the test is false. New programmers often will forget this, sometimes with unanticipated results. If the test is false, the program ignores everything after THEN and drops down to the next line.

If the test is true, the progmm executes everything after THEN and then proceeds to the next line. One of the limitations is that if the statement is true, everything you want to do might not fit on one line. You will see an example of this in this month's listing. To avoid this problem, you will need to change your test, and use the decision to jump around the statements you want executed:

10 IF XX<>1 THEN GOTO 40:REM you want to execute lines 20 and 30 only if XX = 1(<>means unequal)
20 PRINT "Hello there, reader of New Owner's Column"
30 PRINT "You got here because XX=1!"
40 REM pick up here regardless of the value of XX.

You will get used to these programming methods as you as you practice your new skills.

FOR/NEXT/STEP

Often, in a BASIC program, you will want to execute a set of statements many times. For example, you may want to PRINT "Oh, hello there" on the screen 50 times. You could wear out your frngers punching in 50 lines, but there is a much better way!

The answer? Use the FOR/NEXT/STEP commands to do it for you. These commands will allow you to specify which statements are to be part of the loop, and how many times you want the loop executed. Since there are three commands, we will discuss this construct in three parts.

The FOR command defines the start of the loop and also sets the number of times the loop will be executed. A variahle is then used to keep track of how many times the loop executed. To execute a loop 10 times, you might code the following:

10 FOR LOOP=1 TO 10

The first time through the loop, the variable LOOP is equal to 1. The next time it will be equal to 2, and so on, until it reaches 10. When the variable moves outside the range specified (1 to 10 in this example), then the loop ends and program continues by executing the statement following the NEXT command. (More on this in a moment).

In our example, LOOP will reach 11 and then the loop will terminate. You can use the value of LOOP in the executed statements as part of the loop:

10 GRAPHICS 7:COLOR 1
20 COLOR 1:FOR LOOP = 10 TO 75:PLOT LOOP/2,LOOP:NEXT LOOP

Notice that the variable (LOOP in this case) does not need to start at 1. In fact, the variable does not even need to be an integer--FOR LOOP = .236 TO 10.11 will work just fine.

The end of the loop is denoted by the NEXT statement, as in line 20 above. The name of the loop variable from the FOR statement must also appear in the NEXT statement, again as it does in line 20: NEXT LOOP. Everything between the FOR statement and the NEXT statement will be executed as part of the loop. When the loop is finished, execution of the BASIC program will continue with the command following the NEXT statement:

10 REM a short example
20 FOR ROUNDNROUND = 10 to 100:REM loop variable can have any valid name.
30 PRINT "Variable is now ";ROUNDNROUND
40 NEXT ROUNDNROUND:PRINT "Loop finished": PRINT "Variable is now "; ROUNDROUND

Note that the final value of the variable ROUNDNROUND is 101. As this is outside the range of 10 to 100, the loop ended. Also note that execution of the program continued with the PRINT statement following NEXT ROUNDNROUND, even though the PRINT statement is on the same line as the NEXT statement.

You are not limited to changing your loop variable by each time. The STEP command will let you change your loop variable by any increment you want. If you leave out the STEP command, as we have in all the examples so far, then the default value of STEP 1 will be used by BASIC. Any other value of STEP must be specified. Decimal fractions can be used:

10 FOR XXX=1 TO 10 STEP .1

This example will execute 100 times as XXX becomes 1.1, 1.2, etc.--until the loop ends when XXX=10.1.

STEP can also be negative:10 FOR XXX = 10 TO 1 STEP -1:REM Blastoff

This line counts down from 10, ending the loop when XXX = O, which is outside the range of 10 to 1. STEP can be another variable or even be calculated: 10 FOR XXX = 1 TO 100 STEP (YYY+1.2)

Finally, if you use a STEP value of 0, then the loop will never be terminated, since the variable will never change!

HANGMAN

This month's listing is a game that plays just like the old paper-and-pencil standby, Hangman. The computer chooses a word, and prints the number of spaces that correspond to the number of letters in the word. You must try to figure out the correct word by guessing letters.

If you guess a letter which is in the word, that letter is placed in the appropriate blank space in the word. If the letter you guess is not in the word, then a piece of the poor fellow is drawn on the gallows. The letters you have guessed are printed across the bottom of the screen in case you forget. This goes on until you either guess all the letters in the word or you run out of chances.

The game illustrates how to use the IF/THEN and FOR/NEXT statements as part of a complete, functioning program. It also tests your knowledge of the terms important to your Atari. To add your own words, simply add more lines to the end of the program in the following format:

linenumber DATA word

Linenumber refers to a line number greater than the last line number in the program, DATA should be typed in just as shown, and word represents whatever you choose as your new word. Always make sure that the last line of this program contains the word END after the word DATA. This tells the program that all the words have been used. The program also keeps track of how many letters you needed to guess the word, and the percentage of correct words.