“Incorrectly” nested FOR NEXT loops

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
User avatar
1024MAK
Posts: 5104
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

“Incorrectly” nested FOR NEXT loops

Post by 1024MAK »

Way back when I was young, and I was experimenting with BASIC programming, I had to try some of the things that you are not supposed to do. This included mis-using FOR NEXT loops including the kind of thing shown below:

Code: Select all

10 FOR A=1 TO 3
20 FOR B=1 TO 3
30 PRINT A,B
40 NEXT A
50 NEXT B
I had forgotten about this behaviour in ZX81 and ZX Spectrum BASIC, as I myself did not find it useful in any of the code that I wrote. I was reminded about this when reading through a topic elsewhere.

Has anyone used this behaviour in a real program?

Has this been discussed here before?

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
mcarlson_sb
Posts: 122
Joined: Wed Apr 08, 2020 4:48 am
Location: San Francisco

Re: “Incorrectly” nested FOR NEXT loops

Post by mcarlson_sb »

That's nifty.
I'll have to play with it to see how it acts.

Seems like you might be able to exploit it as a counted GOSUB if NEXT B is gated (and possibly moved later in the program).

This is the sort of thing I teach people not to do for readability and maintainability reasons. Which makes it that much more fun to think through.

Now I'm going to have to play :D
User avatar
1024MAK
Posts: 5104
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: “Incorrectly” nested FOR NEXT loops

Post by 1024MAK »

I won’t post up the results then :mrgreen:

Since posting, I also tried it on a emulator running as a ZX80 and got the same result.

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
mcarlson_sb
Posts: 122
Joined: Wed Apr 08, 2020 4:48 am
Location: San Francisco

Re: “Incorrectly” nested FOR NEXT loops

Post by mcarlson_sb »

I was wondering if A was going to overflow.
My first assumption was that it would.

That's a fun one.
Kids, don't do this in production code :lol:
mcarlson_sb
Posts: 122
Joined: Wed Apr 08, 2020 4:48 am
Location: San Francisco

Re: “Incorrectly” nested FOR NEXT loops

Post by mcarlson_sb »

Just for fun I tried this one

Code: Select all

10 FOR A=1 TO 3
20 FOR B=1 TO 3
30 PRINT A,B
40 NEXT A
50 LET A$=""
60 IF A$="" THEN INPUT A$
70 IF A$="S" THEN STOP
80 IF A$="B" THEN NEXT B
90 PRINT A,B
100 GOTO 50
Call your shot then try it :mrgreen:
Hint: Make sure to hit B at least 4 times, preferably more
mcarlson_sb
Posts: 122
Joined: Wed Apr 08, 2020 4:48 am
Location: San Francisco

Re: “Incorrectly” nested FOR NEXT loops

Post by mcarlson_sb »

A better example

Code: Select all

10 FOR A=1 TO 3
20 FOR B=1 TO 3
30 PRINT A,B
40 NEXT A
50 IF A=7 THEN STOP
60 IF B=15 THEN STOP
70 PRINT A,B
80 NEXT B
90 GOTO 50
stroebeljc
Posts: 63
Joined: Thu Apr 23, 2020 6:02 am

Re: “Incorrectly” nested FOR NEXT loops

Post by stroebeljc »

This is why the pros use static analysis tools. ;)
John
Post Reply