Dense Pack Basic, and the use of Logical Operators

Publication

Pub Details

Date

Pages

See all articles from Update July 1988

There has been a considerable amount of questions and interest about Dense Pack programming techniques and the use-of-Boolean logic. While browsing through antique ZX publications I came across an interesting article written in 1983 by Sharon Aker (where are you now Sharon?). Since Sharon’s article merges nicely with my feeble attempts to explain the use of Boolean Logic in Basic programming, her excellent article is re-printed. Also interesting is the setting: The time- Sept. 1983. The computer -Sinclair ZX-8I with 1K total RAM. Boolean Logic is useful in programming ANY computer and can provide increased programming efficiency.

The Logical Operators

AND and OR have two distinct usages in Sinclair Basic. One mirrors their use in English and is easily understood; the second is less straightforward, but is an extremely versatile programming technique. NOT, the third logical operator, is unfortunately overlooked or ignored by many beginner programmers; it, too, can be a powerful programming tool.

AND and OR

IF A<10 AND B<10 THEN...
IF A<10 OR B<10 THEN...

In the first example, the command following THEN is executed if both conditions are true (the conditions being A < 10 and B < 10). In the second example, as long as either condition is true, the command will be executed.

AND has a higher priority than OR and is performed first regardless of its position in the conditional statement.

IF X>5 OR Y>5 AND Z>5 THEN...

This statement sets up two conditions:

  1. X>5
  2. Y>5 AND Z>5

Since they are linked by OR, either one being true will cause the command to be executed.

To circumvent the computer’s automatic ordering of operations, parentheses should be used.

IF (X>5 OR Y>5) AND Z>5 THEN...

The two conditions in this statement are:

  1. X>5 OR Y>5
  2. Z>5

Since they are linked by AND, both conditions must be true for command execution. You should note that, in this last example, there are actually two combinations that will satisfy the computer’s truth-check:

  1. X>5 AND Z>5
  2. Y>5 AND Z>5

Instead of IF-THEN

AND and OR also have another, entirely different, usage in Sinclair Basic, one that allows you to combine several IF-THEN statements into one logical statement that does not even use IF- THEN.

IF A<10 THEN GOTO 125 
IF A=10 THEN GOTO 300
IF A>10 THEN GOTO 480

can be rewritten:

GOTO (125 AND A<10) + (300 AND A=10)+ (480 AND A>10) 

To translate this into understandable English, read each AND as “if.” The computer looks at each one of the parenthetical statements and checks the truth of the expression following AND. If that conditional statement is true, the value of the parenthetical statement is set at whatever precedes AND; if the expression is false, the statement is assigned a 0.

So in this last example, if A is 14, the line will work out as in Figure 1. The computer will GOTO 480.

Figure 1

GOTO(125 AND A<10)+(300 AND A=10)+(480 AND A>10)
falsefalsetrue
GOTO(0)(0)(480)

Figure 2

GOTO(100 AND N<10)+(150 AND N=10)+(200 AND N<20)
falsetruetrue
GOTO(0)(150)(200)

Mutual Exclusivity

The parenthetical statements in the above example are mutually exclusive; that is, if one is true, the others have to be false. That is not always the case, and you should plan carefully in a situation like the following:

IF N<10 THEN GOTO 100 
IF N=10 THEN GOTO 150
IF N<20 THEN GOTO 200

Obviously, N can be less than 10 and less than 20 at the same time; so can it be both equal to 10 and less than 20. As long as the IF-THEN statements are in the right order in your program (“right” depends on the effect you want), there is no problem because the computer will act upon the first true statement it encounters and never see the one(s) following. However, if you rewrite these statements using the logical operators, you would have a big problem. If N is 10, the computer would make the evaluation found in Figure 2 and execute GOTO 350.

Further definition of one or more of the conditions, until the parenthetical statements are mutually exclusive, is necessary. For instance:

IF N<10... 
IF N=10...
IF N<20 AND N>10...

Written logically,

GOTO (100 AND N<10)+ (150 AND N=10) + (200 AND (N<20 AND N>10)) 

In the last parenthetical statement, the first AND operates as “if.” The second AND operates in its usual manner requiring that both conditions be true for the total expression to be true. The inner parentheses are not strictly necessary, since the first AND will always be used as the “if”; they were included here only to make the example clearer.

Other Commands

Since the entire logical expression boils down to a number, it can be used in place of a number with many different commands:

PRINT AT (5 AND Z>14)+(2 AND Z<5),10; "OKAY"
PLOT X,(17 AND B=12)+(27 AND B<>12)
GOSUB (500 AND L=10)+(800 AND M=10)
PRINT (0 AND N<>10)+(N AND N=10)
PAUSE (120 AND T<10)+(380 AND T>=10)

Strings

Strings can also be used with logical statements. If the conditional expression is true, the parenthetical expression is “equal to” the string before AND. If the conditional expression is false, the state- ment is considered to be an empty string. So:

IF A>B THEN PRINT "TOO HIGH"
IF A<B THEN PRINT "TOO LOW"

can be rewritten as:

PRINT ("TOO HIGH" AND A>B)+("TOO LOW" AND A<B)

When A is larger than B, the statements will be evaluated:

PRINT ("TOO HIGH")+("")

You can also assign a string value with this syntax:

LET P$=("TOO HIGH" AND A>B)+("TOO LOW" AND A<B)

A logical expression for strings or numbers does not have to consist of alternative choices; a parenthetical logical expression can be inserted into an otherwise straightforward command. Consider a program that would display a multiplication problem of two randomly generated numbers (A and B) and a player’s answer (C). If the answer is wrong, you might want it marked with an asterisk.

PRINT A;"*";B; TAB 10;("*" AND C<>A+B); TAB 11; C

If the answer is correct, nothing will be printed at TAB 10.

Variable Re-valuation

Logical expressions that change the value of a variable are easy to use. Here is one example, and the ways it would be evaluated.

LET X=X+(5 AND B<A)+(7 AND B>A)
IF B<A X+(5)+(0)=(X+5)
IF B>A X+(0)+(7)=(X+7)
IF B=A X+(0)+(0)=(X)

As you can see, there is no need to write a statement for the B=A possibility if you want X to remain the same in that situation.

If the variable is to be decremented, change the plus sign to a minus sign:

LET N=N+(5 AND P=0)-(5 AND P<>0) 

This will be evaluated in one of two ways:

IF P=0: N+(5)-(0)=N+5
IF P<>0: N+(0)-(5)=N-5

Using OR

While AND is used for strings and numbers, and variable re-valuation involving addition and subtraction, OR is only practical for re-valuations involving multiplication or division. A study of the chart in Figure 3 might help you understand why this is so.

When OR is used in this construction, read it as “unless”:

LET N=N*(10 OR A>B) 

If B is less than A, the statement is true. It is evaluated as 1 (as noted in the chart), and reads:

LET N=N*(1) 

and the value of N does not change.

If B is not less than A, the parenthetical statement is assigned the value of the number before OR:

LET N=N*(10) 

So, N is multiplied by 10 unless B is less than A, in which case N remains the same.

If you were writing a program to calculate new prices for merchandise going on sale, where items less than $100 were to be reduced by 10% and all others by 20%, your statement would read (P is the current price):

LET P=P*(.8 OR P<100)*(.9 OR P=100) 

The evaluations are:

IF P<100: P*(1)(.9)=P*.9
IF P>=100: P*(.8)(1)=P*.8

Applications

If you would like a command executed every second time it is encountered in a loop, use NOT to change the true/false value of a variable back and forth. With:

IF V THEN...

at the beginning of a loop, and:

LET V=NOT V 

later in the loop, V will be true on every other loop.

By using

LET E=N/2-INT(N/2) 

E is zero whenever N is an even number. To have a command executed only when N is even, use

IF NOT E THEN...

Similarly,

LET I=N-INT N 

means that I is zero whenever N is an integer, and

IF NOT I… 

will be a true condition when N is a whole number.

Priority

NOT is assumed to apply only to the number to its right, unlike the other logical operators, which automatically apply to an entire expression.

NOT B<C 

is interpreted as

(NOT B)>C

NOT has priority over both AND and OR, so the following conditional statement would be processed in the order of innermost brackets first:

IF [I [NOT A] AND B] OR C] 

The use of NOT can, of course, be altered by the use of parentheses.

NOT

To make use of NOT, you must first understand the computer’s view of “truth.” Conditions in an IF-THEN statement are evaluated, and a true codition is assigned a one, while false one are assigned zero. (Note the “result” column in Figure 3.)

Furthermore, any mathematical expression whose result is zero is considered false, while a non-zero result (even a negative number) is true.

NOT changes the true/false value of an expression:

If A is false, NOT A is true.
If A is true, NOT A is false.

Figure 3

OperatorConditional ChoiceConditional ExpressionResult
AND
AND
A$
A$
true
false
A$
“”
AND
AND
N
N
true
false
N
0
OR
OR
N
N
true
false
1
N

Products

 

Media

 

Image Gallery

Source Code

Scroll to Top