For Retro Computing Enthusiasts
,

Hints & Tips: Accelerated IFs

By Kurt Carpenter — originally published in COMPUTE!’s Gazette Volume 1, Issue 1

If you’ve discovered a clever time-saving technique, or a brief but effective programming shortcut, send it in to “Hints & Tips,” c/o COMPUTE!’s Gazette for Commodore. If we use it, we’ll pay you $35.

Microsoft BASIC for the VIC-20 and Commodore 64, and probably for other computers as well, will always execute an entire IF-THEN statement before rendering a decision. For example:

IF (A = 1 AND B = 2 AND C = 3) THEN X = 1

In this statement, even though A is not equal to one, B is still checked for two and C is checked for three before this statement is found to be false.

Therefore, you can speed up a program by replacing an IF statement of this type with multiple IF statements, including the most likely to fail first, the next most likely second, etc. The above example would be changed to:

IF A = 1 THEN IF B = 2 THEN IF C = 3 THEN X = 1

This costs only one byte of memory per each additional IF. In programs where execution speed is critical (such as games), the time savings are worth it.

Companion disk Companion Disk (.d64)

Enjoyed this? It’s what we do every month.

The revived COMPUTE!’s Gazette brings the spirit of the golden era to today’s retro scene — Commodore, Atari, Amiga, and more, in full-color print and digital every month.