Classic Computer Magazine Archive Article from Compute! magazine

The Logical Alternative

True-False Logic in Atari BASIC

Ronald R. Lambert

As this article demonstrates, there's a compact and efficient alternative to conventional IF-THEN statements: logical comparisons. The techniques described here work with Atari BASIC on the 400/800, XL, and XE computers—and, with slight adjustments, with all versions of BASIC.

Anyone who has read a BASIC reference manual knows about logical operators such as >, <, =, AND, and OR. These are most commonly used in IF-THEN statements:

IF X>0 THEN PRINT X

(X will be printed only if it is greater than zero.)

But there is another way to use logical statements, one that can streamline and shorten programs considerably—especially in Atari BASIC, which allows calculated GOTOs, GOSUBs, and RESTOREs.

BASIC tests logical statements to see if they are true or false. In keeping with the principles of Boolean algebra, the value 1 is applied to a statement if it is true, and a 0 is applied if the statement is false. (Some BASICs, such as those found on Commodore computers, the IBM PC and PCjr, and Texas Instruments TI-99/4A, apply a –1 if the statement is true.) When the value is true, the statement following the THEN clause in an IF-THEN statement is executed. When the value is false, the program skips to the next line. (Note that the latest BASICs usually let you add an optional ELSE clause to an IF-THEN statement. Execution would then continue with the statement following ELSE.)

The same true-false evaluation also happens with any logical BASIC statement, such as X = 10. Taken by itself (this may require enclosing the statement in parentheses), a statement like X = 10 can be used as a variable—a variable that can equal 1 or 0, depending on whether the equation is true or not. Let's see how we can take advantage of this to shorten a program line.

Logic Versus IF-THEN

Instead of this:

100 IF X=10 THEN Y=Y+1

Try this:

100 Y=Y+(X=10)

If you're using a BASIC that assigns a — 1 to true statements, change the sign of the statement:

Y=Y-(X=10). Subtracting -1 is the same as adding 1.

Both of the above statements mean the same thing and will accomplish the same function: Y is incremented only if X=10. In the second example, IF-THEN is replaced by a logical evaluation. If X does not equal 10, then the statement (X= 10) has an assigned value of 0, and 0 is added to Y—leaving the value of Y unchanged. Only when X does equal 10 will the statement have a value of 1, causing the value of Y to be incremented.

Not only is the second example shorter, but notice the way it is constructed—the program will not skip to the next line if X does not equal 10, but instead can continue on to read further statements in the same program line. In fact, several IF-THEN statements in effect can be combined into one line, as the following two examples demonstrate.

Instead of this:

10 X=X+1:IF X=255 THEN Y=Y+1:X=0

20 IF Y=255 THEN Z=Z+1:Y=0

30 IF Z=255 THEN PRINT "DONE":END

40 GOTO 10

Try this:

10 X=X+1:Y=Y+(X=255):X=X-255* (X=255):Z=Z+(Y=255):Y=Y-255* (Y=255):IF Z<255 THEN 10

20 PRINT "DONE":END

(Remember, if you're using a BASIC that assigns -1 to true statements, reverse the signs in the latter example, except for the statement X=X+1.)

Again, both of the above examples do the same things. They increment Y by 1 every time the value of X reaches 255 (and also reset X to 0), increment the value of Z every time the value of Y reaches 255 (and reset Y to 0), and then when the value of Z reaches 255, print the message DONE.

In the second example, where logic is used, the statement (X=255) is multiplied by 255 and subtracted from X. As long as X does not equal 255, the value of the statement will be zero. Since 255 times 0 is 0, then 0 is what is subtracted from X, leaving the value of X unchanged. But when X equals 255 and the equation is true, then we have 255 times 1 (or — 1, depending on your computer), which is 255. If X equals 255, then subtracting this value from X changes the value of X to 0. (If you're using a BASIC that assigns -1 to true statements and have changed the signs in the above statements as noted, then —255 will be added to X when X equals 255. Adding a negative number is the same as subtracting.)

The same is true for the statement Y=Y-255*(Y=255). In effect, four conditional statements have been combined into one line.

Logical Branching

As mentioned earlier. Atari BASIC allows calculated GOTOs, GOSUBs, and RESTOREs. When logical statements are used in these calculations, it is possible to branch to any line in the program depending upon which logical statement is true. This can save substantial amounts of memory. Consider the following program:

10 OPEN #2,4,0,"K:"

20 ? "Type S, L, or P.":GET #2,N:IF N=83 THEN 60

30 IF N = 76 THEN 70

40 IF N=80 THEN 80

50 GOTO 20

60 ? "This could be a save to tape or disk routine.":GOTO 20

70 ? "This could be a load from tape or disk routine.":GOTO 20

80 ? "This could be an output to printer routine.":GOTO 20

If we use logic, the program can be substantially shortened. Delete lines 30, 40, and 50, and replace line 20 with this:

20 ? "Type S,L, or P.":GET #2,N:GOTO 20+40* (N=83)+50*(N=76)+60* (N=80)

The program works exactly the same as before.

Timing Tradeoffs

Substituting logical statements for IF-THEN statements usually slows down an Atari BASIC program, though normally the difference is too slight to matter, especially when the line is executed only once or just a few times. But the difference is measurable when the statements are enclosed in loops.

To demonstrate, following is a short program that counts words in a long text string. (Actually it counts spaces, an easy way to get a fairly accurate word count.) It generates a long string of text, then uses two of the Atari's internal clock registers to time the two methods for counting. First the words are counted using a conventional IF-THEN construction, and then they're counted using logic.

When you type in and run this program, it displays for you the word counts and the time required for each count measured in jiffies, which are equal to 1/60 second. In this case, the IF-THEN routine (line 60) runs a little faster than the logical statement equivalent (line 100).

Now that you know how logical statements work, you may take a shine to the kind of programming techniques they make available. They certainly provide a logical alternative.