I have a subroutine with a for/next loop in Extended Color BASIC on the TRS-80 Color Computer that I would like, sometimes, to return early from. That is, to return from the subroutine from within the for/next loop and before the for/next loop is completed.
My question is, what effect does exiting a FOR/NEXT loop early have on the TRS-80 Color Computer in Extended Color BASIC?
I thought that exiting from a for/next loop early is bad because it leaves a FOR on a stack, and eventually that stack will fill up if the loops aren’t allowed to complete.
My first experiment was:
1 REM TEST FOR/NEXT WITH RETURN 10 PRINT "HELLO" 20 GOSUB 100 30 PRINT "FINISHED", MEM 40 NEXT I 99 END 100 FOR I=1 TO 20 110 IF I>10 THEN RETURN 120 PRINT I, 130 NEXT I
NEXT I on line 40 generates an NF error. That is, Next without For. Thinking that perhaps the For/Next stack was cleared on a
RETURN to the point it was at the time of the
GOSUB, I tried another experiment using nothing but
GOTO to exit the loop:
10 CLS 20 FOR I=1 TO 3 30 FOR J=1 TO 3 40 FOR K=1 TO 3 50 PRINT@128,L,MEM 60 GOTO 80 70 END 80 L=L+1 90 GOTO 20
I let this run all night in XRoar;
L reached 766750 and the program was still running in the morning. (
MEM didn’t change from the second iteration on to the end.)
What are the effects of exiting a For/Next loop early in Extended Color BASIC? How can I see these effects in a program? And, what, if any, are the side effects of returning from a subroutine from inside an unfinished For/Next loop?