Truth in Programming

See all articles from SYNC v1 n2

(LET X = Y = Z = W)

It’s time for a little truth in programming, time for a bit of logic. Why let your programs slave away with boring numbers when they can deal with such fascinating concepts as truth and falsity? The logical capabilities of the Sinclair represent one of the best ways to shorten and speed up programs. Many programers don’t make full use of these Boolean operators, mainly because, while they are not difficult to use, they are different from other parts of Basic. Boolean logic uses operators such as “AND” and “OR.” The major ways to use these operators will be discussed below.

Logical operators deal with the concepts “true” and “false”. In the Sinclair, true is represented as -1, false is represented as 0. This is just a convention; some machines use 1 for true, some use 0 for true and 1 for false. Knowing how the Sinclair represents true and false, the next question is, what makes something true or false? In Basic, any expression or variable with a value of 0 is false, any other value is considered true.

There are many ways to use logical operators. Let’s start with a simple example. Take the statement 10 IF X > 0 OR X < 0 THEN GO TO 50. This could be replaced with 10 IF NOT X = 0 THEN GO TO 50. But there is an even shorter way to perform the operation. We want to take the branch whenever X is not equal to 0. And, if X is not equal to zero, X is considered to be true. Using this, we can perform the function with the following line: 10 IF X THEN GO TO 50. The IF…THEN statement makes the branch only when the IF part is true. If X is not equal to 0, X is true, so the branch will be taken. Only when X is zero will the branch not be taken. Conversely, the statement 10 IF X = 0 THEN GO TO 50 can be replaced with 10 IF NOT X THEN GO TO 50. You can use this technique to check against numbers other than zero by changing the variable to an expression. For example, IF NOT X = 15 THEN GO TO 100 is equivalent to IF X – 15 THEN GO TO 100. When X is not equal to 15, the expression X – 15 will evaluate as true, and the branch will be taken. What the above example boils down to is this: the IF…THEN statement can be used with any logical operation.

Another important aspect of logical operators is their ability to provide a value that can be used to advantage in a program. Remember that true is represented as -1 and false is represented as 0. Take the following programming problem: a player has to guess whether a number is odd or even. The program must determine whether the guess is correct. The first step would be to get the guess. This could be done with:

10 PRINT "ODD OR EVEN?"
20 INPUT G$

Now, some way has to be used to compare the guess with the number. This brings up another problem: what is the simplest way to determine whether a number is odd or even? In integer arithmetic, an easy way is provided by the fact that division rounds off a number to the integer remainder. The result of dividing 7 by 2 would be 3. This gives a way to determine whether the number is odd or even. For any integer N, if N/2 = N – N/2, then the number is even. Try this with an odd number, then with an even one. N/2 will only be equal to N – N/2 when N is even. Now we could use lines such as 30 IF N/2 = N – N/2 AND G$ = “EVEN” THEN GO TO 100, where line 100 handles a correct guess. But the full comparison of guesses in this manner seems somewhat long and inelegant. Instead, we can take advantage of the values given to logical operations. It happens that the CODE for E is 42 and the CODE for O is 52. If we can find an expression that produces a value of 42 when the number is even, and 52 when the number is odd, we can compare this value with CODE (G$) to determine whether the guess is correct. This can be done using the fact that (N/2 = N – N/2) is true when N is even and false when N is odd. Since true gives a value of -1 and false a value of 0, the comparison can be done with the following line:

30 IF CODE (G$) = 52  (N/2 = N - N/2) *
10 THEN GO TO 100

Since even numbers will cause the expression in the parentheses to be evaluated as -1, the result will be 52 -1* 10 thus matching the CODE for E. If the number is odd, the result will be 52 +0* 10, matching the CODE for O.

This concept bears repeating: airthmetical operators can be used to return logical values. All in all, a very valuable technique. It can be used to adjust scores in games or alter results depending on answers. Since false returns a value of 0, and multiplying any number by zero results in zero, when you add a logical expression multiplied by a constant to a number, the number only changes if the expression is true.

The equal sign can also be used more than once in a line. This fact caused a bit of confusion when mentioned in the ad for SYNC magazine, and deserves a fuller explanation. LET X = Y = Z is a valid expression, but does not assign to both X and Y the value of Z. These variables must be defined first. (To take an analogous case, LET X = X 1 is valid, but will cause an error if X hasn’t been defined.) What the expression does is compare Y to Z and give X the logical value of this comparison. If Y equals Z, X will be assigned the value of -1. This fact can be exploited when a program requires several IF…THEN statements that all use the same comparison. For example:

10 IF Y = Z THEN LET T = T + 1
20 IF Y = Z THEN PRINT "YOU ARE
RIGHT"

Assume that several more uses of this comparison are made later. These can be replaced with:

10 LET X = Y = Z
20 IS X THEN LET T = T + 1
30 IF X THEN PRINT "YOU ARE
RIGHT"

In the above example, no space was saved. But if the program makes repeated use of the comparison, this trick will save space since the expression IF X is shorter than the expression IF Y = Z. Also, the shorter version will execute more quickly since it has less to perform.

While arithmetical operators can be used to produce logical values, there are also certain functions that are used specifically for logical operations, namely AND, OR, and NOT. AND is considered true only when all expressions linked by it are true. OR is true if at least one of the expresions linked by it is true. So the statement IF X AND Y AND Z THEN GO TO 100 will take the branch only if all three variables have a value other than zero. If any one of the variables is zero, the expression will be false. Again, the value produced by this expression can be used in arithmetical operations. The statement LET A = (X AND Y AND Z) * 10 will give A the value -10 if X, Y, and Z are all true. If any of the three are false, A will have a value of 0.

These operators can be combined, producing lines such as IF NOT (A AND B) OR C THEN GO TO 100. While such expressions seem complex at first glance, they become simple when evaluated in sections. The first step would be to look at anything in parenthhses. A AND B by itself is simple. As stated before, this expression is true only if both A and B are true. NOT (A AND B) has the opposite value of (A AND B). So, if (A AND B) is false, NOT (A AND B) will be true. Now, the entire expression can be understood. It is true if either NOT(A AND B) is true or if C is true. To test your understanding of this, determine, before looking at the next paragraph, what logical values of the variables would make the expression false.

Since the major connective in the expression is an OR, the expression will be false only when both sides of the OR are false. So C has to be false. NOT (A AND B) must also be false. NOT (A AND B) is fale when (A and B) is true. (A and B) is only true when both A and B are true. So, when A is true, B is true, and C is false, the expression will be false. With any other values, it will be true.

Logical operations make an excellent addition to a programmer’s bag of tricks. They can be used to shorten programs, speed up programs, and to perform things that would otherwise require great difficulty.