Classic Computer Magazine Archive COMPUTE! ISSUE 46 / MARCH 1984 / PAGE 139

RELATIONAL OPERATORS

Eric Brandon

Relational operators can make your BASIC programs more efficient. Here are some techniques which use relational operators on the Commodore, Atari, TI, Apple, IBM PC and PCjr, Color Computer, and Timex/Sinclair machines.

BASIC has a very useful, but little-known feature. A relational expression such as 2 + 3>4 is interpreted by BASIC as a value of -1 (or 1, depending on the computer) if the expression is true, and a value of 0 if the expression is false. On all Commodore machines, the TI-99/4A, the Color Computer, the IBM PC and the PCjr, a relational expression which is true gives a value of-1. A relational expression which is true on the Atari, Apple, and Timex/Sinclair computers produces a value of 1. A value of 0 results for a relational expression which is false on each computer.

As an example, enter PRINT 2 = 2. You should get a result of -1 (or 1) since the expression is true. Now type in: PRINT 2 = 3. This time, the result is 0 because the expression is false.

Related to this is the fact that the statement

IF Q THEN 100

will be interpreted identically to the statement

IF Q<>0 THEN 100

Can you see why? Both expressions evaluate as true, if Q is nonzero.

Cycling A Variable

Suppose you wanted to continually cycle a variable, say J, from 1 to 10. One way to do this would be:

10 J = 0
20 J = J + 1
25 PRINT J
30 IF J<10 THEN 20
40 GOTO 10

However, by using a relational expression, we can do this:

5 N = -1 : REM N = -l FOR TRUE (MAY BE 1 DEPENDING ON YOUR MACHINE)
10 J = 0
20 J = J*(J<10)* N + 1
25 PRINT J 40 GOTO 20

In this routine, N must be defined as +1 or -1, depending on your machine. Of course, there's really no need for a separate statement to define N. You could easily incorporate the value of N into the expression in line 20. If a true statement produces a -1 on your computer, line 20 becomes J = -J*(J<10) + 1. In this case, as long as J is less than 10, BASIC returns a value of-1 for (J<10). So, -J times -1 plus 1 increases the value of J by one. When J reaches a value of 10, (J<10) gives a value of zero. Adding one to zero starts the cycle over again.

Note that the relational operators are the last items to be resolved. Recall that numeric arguments are resolved in this order: *, /, +, -. This can be easily demonstrated by these two examples: PRINT 2*3 = 3. This gives a result of 0 since it is equivalent to PRINT 6 = 3.

Now try PRINT 2*(3 = 3). This gives -2 (or 2) since it is equivalent to 2*(-l) [or 2*(1)].

More Efficient Tabulation

For another example, suppose you wish to tabulate a score in a math drill program within a subroutine beginning at line 100. A scoring scheme is devised so that the player is awarded a greater number of points the more problems he has solved. You would like the player to get 100 points for each of the first five correct answers, and 1000 points for any correct answers thereafter. If we let X be the total number of correct answers, a common way of doing this would be:

99 REM SCORING SUB
100 IF X>5 THEN 130
110 TALLY = TALLY + 100
120 GOTO 140
130 TALLY = TALLY + 1000
140 RETURN

Using relational operators, however, we can shorten this to (defining N as +1 or -1 as before):

99 REM SCORING SUB
100 N = -l
110 TALLY = TALLY + (X<6) * 100 *N +(X>5) * 1000 * N
120 RETURN

Fewer IF-THEN Statements

Still another example: If you want to transfer program execution to line 1000 if the value of variable I is 100, and to line 2000, if I is 500, several IF-THEN statements would usually be required:

100 IF I = 100 THEN 1000
110 IF I = 500 THEN 2000

On most machines, this can be easily done with relational operators as:

90 N = -l
100 ON N * (I = 100) + N * 2 * (I = 500) GOTO 1000, 2000

On the Timex/Sinclair, since the ON-GOTO statement is not supported in BASIC, you would use GOTO with a conditional expression in the following manner (N = 1, so it's not included here):

100 GOTO (I = 100) * 1000 + (I = 500) * 2000 + (I<>10
    0 AND I<>500) * 200
200 REM RETURN TO MAIN LOOP OF PROGRAM

If you use this powerful technique with imagination, you will find that your programs can be shorter, faster, and easier to write.