Many BASIC dialects contain a special structure called SELECT CASE which is a specialised version of IF...ELSE IF...ENDIF that's much easier to use and read. NextBASIC has a somewhat similar keyword called ON. Its syntax is as follows:
ON n:<stmt0>:<stmt1>:<stmt2>:...:<stmtn>[: ELSE <statements>]
ON uses the resulting integer of rounding the value of expression n to the closest integer and executes the nth statement of the same line that follows it.
When the statement is executed, ON skips the rest of the line and moves to the next line, unless the statement was a non-returning jump such as GO TO, EXIT, RETURN or ENDPROC. If ON runs out of statements (in other words the integer part of n is greater than the number of statements that follows it, then, if the optional ELSE clause is present, all the statements that follow ELSE are executed otherwise the entire line is skipped. For example:
100 ON x:GO TO @xwaszero:GO TO
@xwasone:STOP: GOSUB 200:
ELSE BEEP 1,0: PRINT "x
was > 3":STOP
110 PRINT "execution
returned":STOP
200 PRINT "I'm in the
subroutine now"
210 RETURN
It's easy to understand that if x is 2 then the program will terminate, if it's 0 it will jump to label @xwaszero, if it's 1 it will jump to label @xwasone, if it was 3 it will jump to the subroutine on line 200, return and continue at line 110 and in every other case the computer will beep and inform us that x was greater than 3 before finally halting. While at first sight this may not display much improvement over the nested IF...ELSE...END IF structure especially since it can only check for values greater than 0, in reality however you can prepare the variable to be checked ahead of time and simply test for that and moreover it is much simpler even typing-wise and more concise.
A final way of making decisions but this time within the confines of a single statement that accepts a parameter is provider via the select operator ? (question mark). This has the following form:
n?(expr0,expr1,expr2...)
If this reminds you a bit of the ON keyword syntax above you'd be correct as based on the value of n the nth expression on the list is evaluated. If you run out of expressions in the list then the last expression is always evaluated. The select operator can take either numeric (be it floating point or integer) or string values in its evaluation list, however they all have to be the same. Although it's not immediately apparent how one could use it in te decision-making process, type and RUN the following example which will make it all clear:
10 INPUT "Enter a number "; n
20 PRINT n?(%5, %10, %20)
30 PRINT n?("n is zero", "n is
one", "n is two", "n is
ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.