Speed Up Basic

See all articles from The Plotter v5 n5
Publication: The Plotter
Pub Details: The Plotter v5 n5 (Volume 5, Issue 5)
Date: May 1987
Pages: 4

This concept was explained in a Computer Astronomy Network newsletter, courtesy of Paul Bingham of Pleasantrees.

The use of logicals can speed up some parts of BASIC programs because the computer does not need to sort out a lot of statements, just find a “1” or “0”. Logical expressions have 2 conditions, true or false, which may be considered “1” or “0”.

A logical assignment statement is usually “=”, “>”, “<“, “<=”, and “>=”, as LET B = (y <= 20) with the logical statement within brackets. This is interpreted as, if y is equal or less than 20 then B will be considered to have the value of “1”. Conversely, B will have the value of “0” if y is greater than 20.

Arithmetic operators can be used with this system as LET B = 10 *(y <= 20)+50. This logical statement produces the results of B = 60 if the expression is true (10 * 1 + 50), and B = 50 if false (10 * 0 + 50).

A good place to use logical expressions is in IF — THEN statement substitution:

 100 IF A = 1 THEN LET G = 20
110 IF A = 2 THEN LET G = 30
.
.
150 IF A = 6 THEN LET G = 70

These lines may be written as a single line as:

 100 LET G = 20*(A = 1) + 30*(A = 2) + 40*(A = 3) + 50*(A = 4) + 60*(A = 5) + 70*(A = 6)

as G can have only one of 6 values, and only one of the 6 can have a value of “true”. In this way G is assigned an integer between 20 and 70.

This system has merit in GOTO statements often used in menu choices, as:

 600 IF D<1 OR D>4 THEN GOTO 600
610 IF D = 1 THEN GOTO 6500
620 IF D = 2 THEN GOTO 6700
630 IF D = 3 THEN GOTO 6800
640 IF D = 4 THEN GOTO 6900

Substitution may be:

 600 GOTO 600*(D <1 )+600*(D >4 )+6500*(D = 1)+6700*(D = 2)+6800*(D = 3)+6900*(D = 4)

Logical operators produce “1” or “2”, so you may recall that multiplying by “1” returns the value multiplied “1”, and multiplying by “0” produces “0”. Thus in the last statement if D = 2 then the other Ds must be “0”. Therefore 6700*(D = 2) is the same as 6700*1 = 6700 and all other values will be “0”.

Using logical operators produces speedier operations than IF — THEN statements plus there is some saving in memory.

As logical operators sometimes becomes confusing it may be easier to first work out program lines with IF — THEN and after proving the program, changing the IF — THENs over to logical operators.