11

An interesting question has come up about how HP Time-Share BASIC handles this code:

10 PRINT "HOW MANY STARS DO YOU WANT";
20 INPUT N
30 FOR J = 1 TO N
40 PRINT "* ";
50 NEXT J
60 END

ANSI/ECMA will print no asterisks if you enter zero at the input. MS will print one because it's bottom tested.

Does anyone have a way to run this under HP?

3
  • 1
    There are simh 3.x simulators for the HP 21xx series, but I presume you want something that's less work? Apr 23, 2022 at 13:50
  • 2
    I looked at all the TS BASIC manuals I could find (there are several editions online), and none of them bother with this detail -- which seems pretty important for a programmer to know. Although perhaps the authors merely thought that it was obvious that "from 1 to 0 by 1" was the empty set; it's only broken languages that do otherwise.
    – dave
    Apr 23, 2022 at 22:09
  • 1
    The manual for Acces/TS basic states on page 2-13: The statements following the FOR statement, and preceding the closest NEXT statement referencing the same control variable, are then executed none or more times…
    – WimC
    Apr 28, 2022 at 19:38

1 Answer 1

15

It doesn't print any:

Connected to the HP 2100 simulator MUX device, line 0

HELLO-B001,RICK
READY
10  PRINT "HOW MANY STARS DO YOU WANT";
20  INPUT N
30  FOR J=1 TO N
40  PRINT "* ";
50  NEXT J
60  END 
RUN


HOW MANY STARS DO YOU WANT?10
* * * * * * * * * * 
DONE
RUN


HOW MANY STARS DO YOU WANT?0

DONE

I got this running in a fairly recent version of simh built from source (make hp2100) then running the 2000E Time-Shared BASIC revision 1534 Release 3 kit from HP 21xx/1000 and HP 3000 SIMH Simulators then following the Using the Disc Image instructions in the included readme.txt. This is a fiddly process, and you likely won't get it right the first couple of times.

If you get lots of Non-existent parameter errors, your simh is too old.

19
  • 1
    Very interesting. Someday I'll have to figure out how they did that... normally an interpreter wouldn't know where to go if the test fails immediately, so either they do a lookahead or they top test and then roll forward to the NEXT instead of the other way around. Apr 23, 2022 at 20:23
  • 2
    @MauryMarkowitz - I don't see why being an interpreter prevents a scan over the source code before execution starts.
    – dave
    Apr 23, 2022 at 22:12
  • 1
    @MauryMarkowitz: there's also no requirement to scan up front. You could do something like have a flag indicating "I'm in a dead for loop" which reads but doesn't execute code. You stay in that mode until you get to the corresponding NEXT (so you'll probably need to stack dead FOR/NEXT pairs) then turn off the flag. Not how I would do it, I'd prefer pre-scan to speed things up.
    – paxdiablo
    Apr 23, 2022 at 23:50
  • 3
    I understand the hesitation, but finding the closest NEXT forward shouldn’t be fundamentally harder than implementing GOTO to a forward line number. Apr 24, 2022 at 0:12
  • 3
    @paxdiablo: "When the value of the simple variable passes the final value": the initial value has already passed the final value if N=0, so the loop is never entered.
    – scruss
    Apr 24, 2022 at 2:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .