Marius Posted January 10, 2021 Share Posted January 10, 2021 Perhaps a stupid question haha, but I was wondering: is it possible to exit a FOR...NEXT loop? The only thing I can think of is this: 10 FOR T=1 to 10000 20 IF PEEK(53279)=6 THEN T=10000; REM checks whether START is pressed 30 NEXT T As soon as START is pressed, T becomes 10000 and the FOR..NEXT loop will be exit on line 30. I am wondering: can this be done less 'drastic' than this? Thanks M. Quote Link to comment Share on other sites More sharing options...
TGB1718 Posted January 10, 2021 Share Posted January 10, 2021 Unfortunately there's no equivalent of "BREAK" in Atari BASIC, usual way out is a GOTO Quote Link to comment Share on other sites More sharing options...
joeybutner@gmail.com Posted January 10, 2021 Share Posted January 10, 2021 8 minutes ago, Marius said: Perhaps a stupid question haha, but I was wondering: is it possible to exit a FOR...NEXT loop? The only thing I can think of is this: 10 FOR T=1 to 10000 20 IF PEEK(53279)=6 THEN T=10000; REM checks whether START is pressed 30 NEXT T As soon as START is pressed, T becomes 10000 and the FOR..NEXT loop will be exit on line 30. I am wondering: can this be done less 'drastic' than this? Thanks M. Been years for me but would a POP statement not do this? 1 Quote Link to comment Share on other sites More sharing options...
Marius Posted January 10, 2021 Author Share Posted January 10, 2021 2 minutes ago, TGB1718 said: Unfortunately there's no equivalent of "BREAK" in Atari BASIC, usual way out is a GOTO That is a good one. Will GOTO break the For Next Loop? With other words, when a GOTO is done, is it possible to start a new FOR NEXT loop with T as counter? Quote Link to comment Share on other sites More sharing options...
Marius Posted January 10, 2021 Author Share Posted January 10, 2021 2 minutes ago, joeybutner@gmail.com said: Been years for me but would a POP statement not do this? That is an interesting command as well! Thank you so much! Quote Link to comment Share on other sites More sharing options...
drpeter Posted January 10, 2021 Share Posted January 10, 2021 (edited) 9 minutes ago, Marius said: That is a good one. Will GOTO break the For Next Loop? With other words, when a GOTO is done, is it possible to start a new FOR NEXT loop with T as counter? To do this 'properly', you need to first POP then GOTO. The POP does not change the sequence of execution, it removes the loop address for the FOR/NEXT loop from temporary storage ('the stack') which would normally be cleaned up when the loop completes. Having dealt with housekeeping, the GOTO then jumps you out of the loop. You need to POP once for every loop you are jumping out of- so if you are in the innermost of 3 nested FOR/NEXT loops and are using GOTO to jump beyond the limits of all 3, you need to POP:POP:POP:GOTO .... Edited January 10, 2021 by drpeter 3 1 Quote Link to comment Share on other sites More sharing options...
joeybutner@gmail.com Posted January 10, 2021 Share Posted January 10, 2021 15 minutes ago, drpeter said: To do this 'properly', you need to first POP then GOTO. The POP does not change the sequence of execution, it removes the loop address for the FOR/NEXT loop from temporary storage ('the stack') which would normally be cleaned up when the loop completes. Having dealt with housekeeping, the GOTO then jumps you out of the loop. You need to POP once for every loop you are jumping out of- so if you are in the innermost of 3 nested FOR/NEXT loops and are using GOTO to jump beyond the limits of all 3, you need to POP:POP:POP:GOTO .... This ^^^^ It has probably been over 30+ years since I looked at Atari BASIC command statements. Thanks for the correction and more detail. Quote Link to comment Share on other sites More sharing options...
thorfdbg Posted January 11, 2021 Share Posted January 11, 2021 21 hours ago, Marius said: That is a good one. Will GOTO break the For Next Loop? With other words, when a GOTO is done, is it possible to start a new FOR NEXT loop with T as counter? No, a GOTO doesn't break anything, but you can start a new FOR-NEXT loop with T as counter. The only issue is that an entry remains on the run-time stack of Atari Basic that, if you repeat doing this, continously eats up memory. However, the next RETURN the code runs into removes any stray FOR/NEXT entries from the run-time stack. The recommended way is to first do a POP, then a GOTO. The POP removes the entry, the GOTO leaves the loop. An alternative approach that also works has been proposed by the OP. 1 1 Quote Link to comment Share on other sites More sharing options...
dmsc Posted January 12, 2021 Share Posted January 12, 2021 Hi! 8 hours ago, thorfdbg said: No, a GOTO doesn't break anything, but you can start a new FOR-NEXT loop with T as counter. The only issue is that an entry remains on the run-time stack of Atari Basic that, if you repeat doing this, continously eats up memory. However, the next RETURN the code runs into removes any stray FOR/NEXT entries from the run-time stack. The recommended way is to first do a POP, then a GOTO. The POP removes the entry, the GOTO leaves the loop. An alternative approach that also works has been proposed by the OP. Also, in nested FOR loops, a NEXT with the outer loop variable will automatically POP the inner loop from the stack: 10 FOR I=0 TO 10 20 FOR J=0 TO 10 30 IF J=5 THEN GOTO 50 40 NEXT J 50 NEXT I Also, a new FOR with the same variable automatically removes all nested FOR from the stack, so you can't nest FOR loops with the same variable: 10 FOR I=0 TO 9 20 FOR J=0 TO 9 30 FOR I=0 TO 9 40 NEXT I 50 NEXT J : REM THIS WILL PRINT "ERROR 13 AT LINE 50" BUT... this will only remove contiguous stack entries, so this will work: 10 FOR I=0 TO 9 20 FOR J=0 TO 9 30 GOSUB 70 40 NEXT J 50 NEXT I 60 END 70 FOR I=0 TO 9 80 NEXT I 90 RETURN Have Fun! 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.