the innermost loop. Note that in such cases, only the final EXIT statement can take the optional parameter. To illustrate, consider the following example:
100 FOR i=1 TO 10
110 FOR j=1 TO 10
120 PRINT i,j
130 IF j*i>80 THEN EXIT:EXIT 170
140 NEXT j
150 NEXT i
160 STOP
170 PRINT "Product exceeded 80":STOP
Note that when in a loop that exists within a procedure and/or subroutine, it is acceptable to use ENDPROC or RETURN as a legitimate way to exit said loop.
NextBASIC has another way of looping: a set of commands (or rather a single command block) called REPEAT … REPEAT UNTIL. You will have noticed that FOR … NEXT relies on counting to control the loop however you can also use a condition to control a loop. This type of loop begins with a REPEAT statement to indicate the beginning of the loop and a REPEAT UNTIL statement at the end, which also contains the condition to exit the loop. Try this:
10 REPEAT
20 INPUT "Enter a number,
or enter -1 to stop > ";n
30 PRINT n
40 REPEAT UNTIL n=-1
50 PRINT "Thank you!"
This program will keep accepting numbers and printing them, until you type -1 when it will politely thank you for your numbers. In a REPEAT … REPEAT UNTIL loop, everything between the REPEAT and the REPEAT UNTIL command will be executed (in this case, this would be lines 20 and 30), until the condition in the REPEAT UNTIL statement becomes true (in this case, that the number you have entered is -1). Note that because the condition is checked at the end, the block of statements will always execute at least once.
The following, for example, would print an erroneous statement:
10 x=1
20 REPEAT
30 PRINT "x is ";x;" but isn't 1"
40 REPEAT UNTIL x=1
50 PRINT "x is now 1."
Because line 30 is executed before the condition is checked at line 40, the message x is 1, but it isn't 1 will still be printed, which is clearly wrong. Like a FOR … NEXT loop, you can also nest REPEAT loops, if you need to. So:
10 n=1
20 REPEAT
30 PRINT "Counting to ";n
40 c=1
50 REPEAT
60 PRINT c;", ";
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.