LoginLogin

Matching NEXTs to FORs

Root / Programming Questions / [.]

SquareFingersCreated:
Imagine you have a program (some code) END ' >> Section 1 >> @Some_label (some code) GOTO @somewhere ' << Section 1 << ' >> Section 2 >> @Another_label (code) GOTO @someplace ' << Section 2 << In *EVERY* dialect of BASIC I have encountered, if you were to switch "Section 1" and "Section 2", the behaviour of the program would not be altered. Not so with SmileBasic. FOR A=1 TO 2 FOR B=1 TO 2 GOTO @NEXTB @TESTEND END ' >> Section 1 >> @NEXTB PRINT "@NEXTB",A,B NEXT GOTO @NEXTA ' << Section 1 << ' >> Section 2 >> @NEXTA PRINT "@NEXTA",A,B NEXT GOTO @TESTEND ' << Section 2 << Run this program, and you get the expected output: @NEXTB 1 1 @NEXTB 1 2 @NEXTA 1 3 @NEXTB 2 1 @NEXTB 2 2 @NEXTA 2 3 Switch the order of "Section 1" and "Section 2", and you get @NEXTB 1 1 @NEXTB 2 1 @NEXTA 3 1 @NEXTB 3 2 @NEXTA 4 2 It appears, before the program starts, every NEXT is associated with a FOR: that is, the FOR that it would normally be associated with if the flow of execution were always from one line to the next, no skipping, no going back. That will work, most of the time, but not all of the time. I believe this behaviour is the result of another design decision made by SmileBasic that is different from every other Basic: that a FOR loop need not ever execute. Someone made the decision to deviate from a constant feature in ALL OTHER BASICS, and as a result, this bug. Poor design decisions are evident... again.

Interesting. I would say it's definitely bad practice to have a NEXT anywhere not on the same 'level' parallel to the FOR, though, so this bug (and yes, I agree it is a bug) should encourage you to learn a good habit. Either way though, after they released I honestly think the worst thing to do is fix something like this. Backwards compatibility is important.

I don't call this bad design. In the context of SB, NEXT isn't used as a keyword that says "continue the loop", it's used as a block terminator like how WEND is to WHILE. If you need something equivalent, use CONTINUE (and sometimes BREAK when need be.) They both work in ALL loops too. Not being the same as other BASIC dialects isn't really a problem.

N-N-N-N-NECROPOOOOOOST ٩(╬ఠ༬ఠ)و

If you find a bug involving GOTO, the problem is that you're using GOTO.