A command found in many versions of Basic, but not in the Sinclair, is ON … GOTO. This is usually found in the form ON X GOTO 110, 120, 130. The command makes a jump depending on the value of X. In this example, if X is 1, the program will jump to 110, if X is 2, control goes to line 120, and if X is 3, the program continues at 130. If X is outside the expected values, the program will fall through to the next line. In other words, for any value N of X, the program will jump to the Nth line listed in the expression.
The simplest way to replace this command is to use a series of IF…THEN statements. The above example is equivalent to
10 IF X = 1 THEN GOTO 110
20 IF X = 2 THEN GOTO 120
30 IF X = 3 THEN GOTO 130
If there are many numbers involved, this process can get tedious. Fortunately, there are other ways to Sync the cat. The Sinclair allows for the use of expressions with a computed GOTO. For example, the above command can be replaced with GOTO 100 + 10 * X. In many cases, you can renumber a translation so the lines used in ON…GOTO will be part of a simple progression. But there are cases where the progression is not simple.
Take a line such as ON X GOTO 90, 450, 376, 10. Rather than look for an algorithm that will produce the correct number, it is easier to set up an expression. Using the logical capabilities of the Sinclair, we can produce an expression that has the desired sum for any X value. What we need is a series where the sum of each member is zero unless it matches the desired X value. When there is a match, the sum will be the value of the desired line for the jump. The above line can be replaced with GOTO ABS ( (X=1) * 90 + (X=2) * 450 + (X=3) * 376 + (X=4) * 10). This expression will produce the desired results. Those parts of the expression where the equality fails will produce a value of 0. When there is a match, the result will be correct except for having a negative value. This is caused by the use in the Sinclair of -1 to signify true. The ABS takes care of that.
Another common Basic operation is the LEN function. The expression LET X = LEN (A$) will give X a value equal to the number of characters in A$. If A$ is HELLO, then X will be 5. This expression has many uses. Once you know the length of a string, you can manipulate it in various fashions. While the Sinclair does not have the LEN function, it does have TL$ which removes the first character of a string. Using TL$ in a loop, the length of any string variable can be determined. The basic approach is to keep chopping off the first character of a string until there is nothing left. If you count how many beheadings have occurred, you will know the length of the string. Here’s one way to do it.
10 INPUT A$
20 LET L = 0
30 LET B$ = A$
40 IF B$ = "" THEN GOTO 100
50 LET B$ = TL$ (B$)
60 LET L = L + 1
70 GOTO 40
100 PRINT A$; "HAS A LENGTH OF"; L
The program is fairly straightforward. Since TL$ destroys the variable, A$ is preserved by using B$ for the operation. When B$ has only one character left, the result of TL$ (B$) will produce a null string (represented in line 50 as a pair of quotes with nothing between them).
That’s all for now. If you have any specific functions you would like to see covered here, drop me a line.