Post Reply 
Useful Programming Trick .. But 40 years late!
01-18-2016, 04:31 PM
Post: #1
Useful Programming Trick .. But 40 years late!
If you still tinker with legacy BASIC (like the one used on the series 80 HP desktop machines) or the hP-71B and the HP-75C, like I still do, then I have a programming trick for you, especially if you dislike the lack of structured IF-THEN-ELSE constructs that don't need line numbers (like the Paris-group BASIC extensions to the HP-71B). You can use the FOR loop to emulate the THEN and the ELSE clauses as follows:

Code:
IF condition THEN L=1 ELSE L=0
REM SIMULATE THEN CLAUSE
FOR I=1 TO L
statements to execute if condition is true
NEXT I
REM SIMULATE ELSE CLAUSE
FOR I=L TO 0
statements to execute if condition is false
NEXT I

You can replace the variables L and I with any other variable names support by the BASIC interpreter you are working with.

Here is an example using the HP-71B:

Code:
10 X=RND
20 DISP "X=";X @ WAIT 3
30 IF X<=0.5 THEN L=1 ELSE L=0
35 REM EMULATE THE THEN CLAUSE
40 FOR I=1 TO L
50 DISP "X<=0.5"
60 NEXT I
65 REM EMULATE THE ELSE CLAUSE
70 FOR I=L TO 0
80 DISP "X>0.5"
90 NEXT I

The above trick is handy if you are coding a short example on the fly and don't want to be bothered with line numbers (or labels in the case of the HP-71B).

Enjoy the very belated programming trick!

Namir
Find all posts by this user
Quote this message in a reply
01-18-2016, 06:20 PM
Post: #2
RE: Useful Programming Trick .. But 40 years late!
Brilliant!

David Brunell
Houston, Texas
Find all posts by this user
Quote this message in a reply
01-18-2016, 07:32 PM
Post: #3
RE: Useful Programming Trick .. But 40 years late!
Something along the lines of macros in assembly language, you could go further and write with the desired structures, in text, with a text editor, then have a program that replaces the structure commands with what you're proposing above, before TRANSFORMing to BASIC. I did something similar it order to get rid of the line numbers (the program that did the TRANSFORMing added them) and to have white space for readability, but did not think of taking it as far as forming such structures. I showed Geir an example of it when we met in L.A. a couple of years ago. The Paris users' group did have a module for the 71 that added multi-line IF...THEN...ELSE...ENDIF, CASE, and maybe some others, but I never got it.

http://WilsonMinesCo.com (Lots of HP-41 links at the bottom of the links page, http://wilsonminesco.com/links.html )
Visit this user's website Find all posts by this user
Quote this message in a reply
01-19-2016, 03:20 AM (This post was last modified: 01-19-2016 10:43 PM by Namir.)
Post: #4
RE: Useful Programming Trick .. But 40 years late!
(01-18-2016 07:32 PM)Garth Wilson Wrote:  Something along the lines of macros in assembly language, you could go further and write with the desired structures, in text, with a text editor, then have a program that replaces the structure commands with what you're proposing above, before TRANSFORMing to BASIC. I did something similar it order to get rid of the line numbers (the program that did the TRANSFORMing added them) and to have white space for readability, but did not think of taking it as far as forming such structures. I showed Geir an example of it when we met in L.A. a couple of years ago. The Paris users' group did have a module for the 71 that added multi-line IF...THEN...ELSE...ENDIF, CASE, and maybe some others, but I never got it.

In 1983 I was using BASICA on the IBM PC. I wrote a pre-processor program, which I called NBASIC, and published in in Dr Dobb's Journal. NBASIC read text files that had structured code with IF-THEN-ELSE, SELECT CASE, and SUBroutines (with parameters), and wrote BASICA-compliant code.

Namir
Find all posts by this user
Quote this message in a reply
01-19-2016, 05:34 AM (This post was last modified: 01-19-2016 04:44 PM by Katie Wasserman.)
Post: #5
RE: Useful Programming Trick .. But 40 years late!
(01-19-2016 03:20 AM)Namir Wrote:  In 1983 I was using BASICA on the IBM PC. I wrote a per-processor program, which I called NBASIC, and published in in Dr Dobb's Journal. NBASIC read text files that had structured code with IF-THEN-ELSE, SELECT CASE, and SUBroutines (with parameters), and wrote BASICA-compliant code.

Neat! I see that you wrote a few articles for DDJ. I remember getting that journal way back in the late 1970's and reading about FORTH, tiny BASIC, etc.. It was so much more in depth than BYTE. But you really needed to get BYTE too if you wanted to keep up with what was going on with small computers back then.

BASICA does indeed have the same FOR-NEXT loop behavior that you show for the 71b and I used to use that feature too in many programs. However many other versions of BASIC from "back in the day" did not work this way. They worked by translating the FOR statement as a simple assignment statement, the increment-test-branch was done in the translation of the NEXT statement. This meant that FOR-NEXT loops were executed at least once no matter what the conditions.

The 1978 ANSI standard for minimal BASIC says FOR-NEXT loops should work as you use them in your trick, but many versions of BASIC came well before that and/or just didn't care to be compliant. This was true for Commodore BASIC for example.

-katie

Visit this user's website Find all posts by this user
Quote this message in a reply
01-19-2016, 07:29 AM
Post: #6
RE: Useful Programming Trick .. But 40 years late!
(01-19-2016 05:34 AM)Katie Wasserman Wrote:  Neat! I see that you wrote a few articles for DDJ. I remember getting that journal way back in the late 1970's and reading about FORTH, tiny BASIC, etc.. It was so much more in depth than BYTE. But you really needed to get BYTE too if you wanted to keep up with what was going on with small computers back then.

I also ended up writing software reviews for BYTE, I did a lot of reviews, articles, and even a column for Computer Language! I recently found pdf files for these (and other old PC/programming related magazines) online.

Thanks for commenting on how some BASIC dialects handled testing the limits of the FOR-NEXT loop.

Namir
Find all posts by this user
Quote this message in a reply
01-21-2016, 08:59 AM
Post: #7
RE: Useful Programming Trick .. But 40 years late!
Just to mess with Structured Programming (as opposed to structured programming):

While (I>0)
Case(I=1).......I=xxx
Case(I=2).......I=xxx
End While

This skeleton will perform arbitrary sets of forward and backward jumps in a Structured Manner.
Find all posts by this user
Quote this message in a reply
01-21-2016, 09:14 AM
Post: #8
RE: Useful Programming Trick .. But 40 years late!
(01-21-2016 08:59 AM)ttw Wrote:  Just to mess with Structured Programming (as opposed to structured programming):

While (I>0)
Case(I=1).......I=xxx
Case(I=2).......I=xxx
End While

This skeleton will perform arbitrary sets of forward and backward jumps in a Structured Manner.

Interesting. Can you give an example of it in practical usage?

http://WilsonMinesCo.com (Lots of HP-41 links at the bottom of the links page, http://wilsonminesco.com/links.html )
Visit this user's website Find all posts by this user
Quote this message in a reply
01-21-2016, 10:02 PM
Post: #9
RE: Useful Programming Trick .. But 40 years late!
The only time I did something similar was in writing a text editor in Fortran. Depending on context, each incoming keystroke may go arbitrary places (insert mode, delete mode, overwrite mode, macro create, etc.)

The main point was that one could use Structured Programming (supposedly to make programs easy to read) and still write arbitrary branching programs.

During Structured Programming's heyday, it was difficult to write some numeric algorithms because iterations may have multiple termination conditions (convergence or iteration limit is a common double condition).
Find all posts by this user
Quote this message in a reply
01-21-2016, 11:45 PM
Post: #10
RE: Useful Programming Trick .. But 40 years late!
(01-21-2016 10:02 PM)ttw Wrote:  The only time I did something similar was in writing a text editor in Fortran.

I can't imagine a less compatible combination of language and application.

Still... Respect!

Fortran was my first love, so it has a special place for me. If all goes well, I hope to get it running on my HP-87XM this year. Still have your source code?

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
01-22-2016, 01:55 AM
Post: #11
RE: Useful Programming Trick .. But 40 years late!
Unfortunately, I've lost the source code over the years. The editor was text oriented (columns and lines), based on the string manipulation language TRIX. It worked well through Fortran I/O. I wrote the keyboard and monitor GUI in assembly for PCs. I also introduced the very fast text searching methods of Horspol and others.

I'd like to rewrite something similar using more modern programming techniques (Fortran 90 and 95 constructs also.) Also I'd like to write the screen stuff in something like HTML or the like. I don't really have time for such a large project on my on.
Find all posts by this user
Quote this message in a reply
06-27-2016, 11:33 AM
Post: #12
RE: Useful Programming Trick .. But 40 years late!
(01-21-2016 08:59 AM)ttw Wrote:  Just to mess with Structured Programming (as opposed to structured programming):

While (I>0)
Case(I=1).......I=xxx
Case(I=2).......I=xxx
End While

This skeleton will perform arbitrary sets of forward and backward jumps in a Structured Manner.

This solution is often used to translate programs from languages which have GOTO statements (e.g. early BASIC) to languages which do not have them (like Java).

The program is translated into a large switch structure as shown, with "case" statements for each a GOTO target and "break" statements for each GOTO. Just before the "break" the switch variable is set to the label (e.g. line number) of the GOTO target.
The while loop simulates the process of running through the original program lines. It is possibly but not necessary to have case statements for each line, they are needed only where jumps occur. Thus you can make any ugly old spaghetti BASIC or FORTRAN program into a "well structured" Pascal program ;-)

"Look Ma: no GOTO!"

Martin
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)