While Basic is a distinct computer language, it exists in many forms. Thus, a program written for another machine might have to be converted before it will work with the Sinclair. This series will deal with conversion techniques, in hopes of allowing you to get as many programs as possible running.
A command found in many Basics is STEP. This is used in FOR-NEXT loops when an increment other than one is desired. For example:
10 FOR = 1 TO 9 STEP 2
20 PRINT I
30 NEXT I
This program will print the odd numbers from one to nine. At each pass through the loop, the variable I will increased by two. STEP can also be negative:
10 FOR I = 10 TO 1 STEP -1
20 PRINT I
30 NEXT I
In this case, I will decrease by one on each pass through the loop.
There is no STEP command in Sinclair Basic. Fortunately, there is an easy way around this. Loops are convenient, but they can always be replaced with IF-THEN statements. This is done by using a variable for a counter, and checking to see whether it is still in the desired range. If it is, the program portion is repeated. A simple loop such as
10 FOR I = 1 TO 10
20 PRINT I
30 NEXT I
could be replaced with
10 LET I =1
20 PRINT I
30 LET I = I + 1
40 IF I < 11 THEN GOTO 20
Line 10 initializes the variable. After the desired action is performed by line 20, line 30 increases the variable. Then, at line 40, the variable is checked to determine whether it is still within the set limits. A value of 11 was used since we want the action to be performed ten times. The variable is increased and checked after the action since this is the way it is done when using FOR-NEXT loops. As might be obvious now, the STEP command is equivalent to the actions performed by line 30. In this case, the STEP was one. For other steps, all that is needed is a change of the values used. For instance, the first example could be rewritten as:
10 LET I = 1
20 PRINT I
30 LET I = I + 2
40 IFI < 10 THEN GOTO 20
For the second example, with a negative STEP, you would use
10 LET I = I
20 PRINT I
30 LET I = I - 1
40 IF I >0 THEN GO TO 20
Notice that when replacing the negative STEP, a > was used, while a < is used in positive STEPS. In either case, you have to check to see if the value has gone beyond the desired range.
Some programs use fractional values for STEP. In some cases you can get around this by multiplying everything by a constant that will make the STEP value an integer. In other cases, where the decimal value is essential, this won’t work.
Future articles will cover other conversion techniques, showing how to get those strange looking programs to run on the ZX-80.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.