# interrupt a FOR...NEXT loop in Atari Basic

## Recommended Posts

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.

##### Share on other sites

Unfortunately there's no equivalent of "BREAK" in Atari BASIC, usual way out is a GOTO

##### Share on other sites
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?

##### Share on other sites
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?

##### Share on other sites
2 minutes ago, [email protected] said:

Been years for me but would a POP statement not do this?

That is an interesting command as well! Thank you so much!

##### Share on other sites
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 by drpeter
• 3
• 1

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites

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!

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.