Back To BASIC: Kemeny & Kurtz's book from 1985

I only just realised how rare pass-by-reference is in modern high-level languages. I knew python doesn’t have it, but I don’t think Lua and JavaScript do either (which I put in a similar mental category).

I wonder if there’s much BASIC code that uses it.

I thought of it as a fairly standard thing since I used it in Pascal, and then when I learnt C it had pointers which almost let you do the same thing.

1 Like

A few years ago, I was working on a BASIC cross-compiler and came upon this very issue.

Discussion here:

https://6502.org/forum/viewtopic.php?f=2&t=6182

and

https://talk.dallasmakerspace.org/t/a-twisted-question-about-basic/72013

2 Likes

And yet, the loop is still executed once by Atari BASIC.

1 Like

Thanks for that link, in which I learned about https://telehack.com with its purported “Dartmouth DTSS TeleBASIC”. Sadly, it’s nothing to do with Dartmouth BASIC and would have the late Professors K most annoyed. While it’s got lots of extra features, it’s missing all of MAT and it does something with FOR loops that the Dartmouth manual says they should never do. Oh well.

1 Like

Yep. I could be wrong, but I’m pretty sure FOR loops in BASIC (according to spec?) were always supposed to execute at least once.

See, that’s the thing: they’re absolutely not. But due to lack of standards and poor documentation, it mostly got ignored by the micro BASICs. Until the first ANSI BASIC standard came out in 1978, this one paragraph from the 1968 Dartmouth manual was about it:

If you write 50 FOR Z = 2 TO -2, without a negative step size, the body of the loop will not be performed and the computer will proceed to the statement immediately following the corresponding NEXT statement.

2 Likes

This feels to me like one of the very biggest distinguishers between Basics. And we’re told I think that Sinclair’s Basic for the Spectrum does honour the zero iteration FOR loop. So it’s not even as simple as all micro Basics.

3 Likes

Was this FOR loop behavior defined in it?

It looks like this is the answer (from Duck.ai) although it doesn’t say when this was specified.

The ANSI BASIC standard specifies that a FOR NEXT loop will execute at least once, regardless of whether its condition is met. This means that the loop’s body will run even if the initial condition does not allow for any iterations.

This note is for everyone:

Please: It’s not good to forward AI content in this way. If you don’t know, you can look things up, or wait for someone who knows.

This forum is a place for people to share their enthusiasm and their experience. Please bring your authentic contributions.

5 Likes

Page 18 of https://ecma-international.org/wp-content/uploads/ECMA-55_1st_edition_january_1978.pdf seems to indicate that if the starting value is past the end value, the loop will not be executed at all.

1 Like

On the FOR loop conundrum… (As someone who’s written a coupe of BASICs)
The original BASIC, as I understand it; is a compiled BASIC. (Although I’m not sure what it compiled into) So maybe easier to ‘fix’ the FOR issue. Or at least detect it.
Simple FOR/NEXT counting doesn’t work as Basic makes it too easy to:

FOR I = 1 TO 10
  IF I = 5 THEN NEXT I
  PRINT I
NEXT I
99 END

One of my Basics uses the construct CYCLE… REPEAT for all loops including FOR but even that can be thwarted as above.

So I don’t think there is an easy solution.

I do provide CONTINUE and BREAK statement for the other loops, but even that relies on the programmer being kind to the system and not having an extra REPEAT buried inside an IF statement. If you do that, then the predictable happens, but it’s not always going to be obvious.

I could scan the tokenised program at RUN time, looking for mismatched CYCLE… REPEATS but on an 8-bit micro counting and matching FOR/NEXT is going to take a little bit of time and precious ROM space - another reason to be “lazy” about it…

Could I add this check into my RTB Basic? Sure - it would be quick (I already do a scan at RUN time to build up a table of procedures & functions and goto/gosub’s - it wouldn’t be hard, but then what do I do when someone comes along and says; “This program I wrote 10 years ago now doesn’t work” …

So maybe we just accept it as it is and work with it.

-Gordon

1 Like

Agreed. Sorry about that.

1 Like

Yeah, I’ve been studying this over the last few weeks. The behaviour is all over the shop with micro BASICs. I’d meant to have a writeup by now. Maybe I’ll manage something from the RV at VCF East.

I tested 38 interpreters and one compiler (though it’s hard to tell if the DEC products were compilers or interpreters, so maybe 3-4 compilers) with release dates from 1970–1992. Every one of them from before the micro age did loops correctly. From about 1975–1984, however, it was the Wild West. After 1984, new BASICs did the right thing.

There was one under-reported event that spurred BASICs to change in the early 1980s. It wasn’t the release of a standard. It was a much more commercial issue.

What I find interesting is how BASICs from the same supplier changed. In the case of Microsoft, you can clearly see several different development/product lines, but regressions being introduced in one product that had long been fixed in others.

1 Like

Sounds interesting! A teaser…

1 Like

I think, this will always be hard for any micro-BASIC with limited resources. Even Commodore BASIC, which allows a GOTO out of the loop, will leave a partially occupied stack behind. (This will be cleaned up, though, if there is a new FOR-loop with the given loop variable. So, if you jump out of any loop, you may want to do an empty FOR-loop on the outer-most loop variable after this.)

Do we know if any of the micro-BASICs which do allow zero-iteration loops do clean up their stack reliably?

3 Likes