Many of you probably have seen the flurry of notes on FOR-NEXT over the last several days. The purpose of this post is to document best practices, so you can ignore all those posts.
I think this is another anti-pattern that should be avoided, particularly if you want it to be portable across BASICs. I hate to say it, but I feel this should be another NO NO on my list. FOR and NEXT, going back to Dartmouth, was intended to be a lexical block structure. In other words FOR appears at one spot, and there is one NEXT further down in the code. A variable name was required in the NEXT to insure a block structure. NEXT was like a curly brace in C. It was not intended to be an executable instruction. It was intended to close a block.
This problem was eliminated in 1983 (for everyone) and earlier (at Dartmouth). Dartmouth BASIC the Sixth and ANSI/ISO Standard BASIC, have an EXIT FOR statement that keeps the FOR-NEXT stack clean. Even Microsoft BASIC has this command now.
AccBASIC and ParactBASIC are compilers and Decimal BASIC is an interpreter. To use the compilers, you have to load the Lazarus Pascal compiler. It's free. Acc and Paract compile to a Pascal program and Lazarus automatically compiles to a true machine language EXE file. You can download either a 32- or 64-bit version of Lazarus (or both) to compile your BASIC programs to 32- or 64-bits. It sounds complicated but once you set the path to Lazarus in the compiler's settings, you don't have to deal with Lazarus again. Just RUN the compiler and it will automatically invoke Lazarus to create the EXE file. Lazarus is very efficient and the code runs as fast, if not faster than most C or C++ programs.
One point of interest - back in the day, having no way to "cleanly" exit a FOR loop, Sinclair BASIC enthusiasts decided that setting the control (or index) variable to greater than the loop's limit was an acceptable practice, and it remains so to this day.
Beside that what should never be done (and it is a bad habit of people using some early microcomputer BASIC implementations) is to use a NEXT without specifying the loop variable.
I think this is another anti-pattern that should be avoided, particularly if you want it to be portable across BASICs. I hate to say it, but I feel this should be another NO NO on my list. FOR and NEXT, going back to Dartmouth, was intended to be a lexical block structure. In other words FOR appears at one spot, and there is one NEXT further down in the code. A variable name was required in the NEXT to insure a block structure. NEXT was like a curly brace in C. It was not intended to be an executable instruction. It was intended to close a block.
This problem was eliminated in 1983 (for everyone) and earlier (at Dartmouth). Dartmouth BASIC the Sixth and ANSI/ISO Standard BASIC, have an EXIT FOR statement that keeps the FOR-NEXT stack clean. Even Microsoft BASIC has this command now.
From: Peter McGavin Gary Luckenbaugh Here's another "illegal" program that you might like to try:
10 I=5
20 GOTO 40
30 FOR I=1 TO 10
40 PRINT I
50 NEXT I
60 PRINT "Done"
70 END
Some like GW-BASIC, PC-BASIC and BBC-BASIC detect the error.
Some like QBASIC, QB45, PB35 and QB64 print "5 Done".
Some like FreeBASIC and mybasic print "5 6 7 8 9 10 Done".
Saves 10 to stack -> detects the error.
Saves 10 to static -> static not initialised so contains 0 -> exits after 5.
Compiler optimises and hard codes the fixed upper limit -> counts to 10.
This isn't even allowed in modern ANI/ISO compilers. They flag line 20.
Where can I find an ANSI/ISO compiler?
https://osdn.net/projects/decimalbasic/releases/
AccBASIC and ParactBASIC are compilers and Decimal BASIC is an interpreter. To use the compilers, you have to load the Lazarus Pascal compiler. It's free. Acc and Paract compile to a Pascal program and Lazarus automatically compiles to a true machine language EXE file. You can download either a 32- or 64-bit version of Lazarus (or both) to compile your BASIC programs to 32- or 64-bits. It sounds complicated but once you set the path to Lazarus in the compiler's settings, you don't have to deal with Lazarus again. Just RUN the compiler and it will automatically invoke Lazarus to create the EXE file. Lazarus is very efficient and the code runs as fast, if not faster than most C or C++ programs.
Lazarus is found here:
https://www.lazarus-ide.org/index.php
One point of interest - back in the day, having no way to "cleanly" exit a FOR loop, Sinclair BASIC enthusiasts decided that setting the control (or index) variable to greater than the loop's limit was an acceptable practice, and it remains so to this day.
Well, two comments.
The first one is that you twice a point #3... ;-)
Beside that what should never be done (and it is a bad habit of people using some early microcomputer BASIC implementations) is to use a NEXT without specifying the loop variable.
Thanks for catching the mis-numbering, I fixed it just now. I agree about the NEXT loop variable. I will add that.