Overlooked Logicals

Authors

Publication

Pub Details

Date

Pages

See all articles from Sinc-Link v4 n4

If you would like to speed up your BASIC programs and increase the available storage, you might consider coding with logical expressions. These are often overlooked program statements that can accomplish the above, and also clean up many GOTOs that often tend to clutter up a program listing.

A logical has two conditions; true or false, which are usually, but not always. always. represented by the values “0” and “1”, respectively, as in Sinclair computers.

An example of a logical assignment statement might be.

300 LET A=(X>=10)

If the value of “X” is equal to or exceeds 10 then the “A” will have a value of “1”. For “X” less than 10, the value of “A” will be “0”.

A logical expression in brackets may be added to and/or multiplied by other values. For example, with the statement:

300 LET A=100*(X>=10)+15

Here “A” takes on the value of 115 when the logical expression is true and 15 when it is false.

To illustrate some of the applications of this the following code was borrowed from “TIMEX/SINCLAIR 1000: ASTRONOMY”, a Sinclair version of “CELESTIAL BASIC” by Eric Burgess, in which day-month calculations. are made:

350 IF M=2 THEN LET D=31
360 IF M=3 THEN LET D=59
...
450 IF M=12 THEN LET D=334

Here, eleven statements must be individually interpreted and evaluated by the computer. The more statements, the longer the program takes to arrive at the results. If the code is repetitively looped, the time is further magnified. Hence if several statements can be combined, fewer interpretations are required, decreasing the program execution time. To illustrate, the following single statement can replace the above code and will execute in about 30% of the time.

350 LET A=31*(M=2)+59*(M=3)+...334*(M=12)

At this point in the program, M has an integer value of between 2 and 12. Since it can only have one of these values, the only one logical will be true. The appropriate value will will then be assigned to since it is the only number multiplied by ’31’.

Since the TIMEX/SINCLAIR allows computed GOTO’s logical can also replace many IF statements. Instead of this:

100 IF A<10 THEN GOTO 50
110 IF A=10 THEN GOTO 500
120 IF A>10 THEN GOTO 600

the following could be substituted:

100 GOTO 50*(A<10)+500*(A=10)+600*(A>10)

As was implied in the opening statement, this method of coding, as well as being more efficient in execution, also occupies less RAM, allowing you to write longer, more complex programs in much less space.

Products

 

Media

 

Image Gallery

Source Code

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

Scroll to Top