Useful Programming Trick .. But 40 years late!
01-18-2016, 04:31 PM
Post: #1
 Namir Senior Member Posts: 822 Joined: Dec 2013
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
 « Next Oldest | Next Newest »

 Messages In This Thread Useful Programming Trick .. But 40 years late! - Namir - 01-18-2016 04:31 PM RE: Useful Programming Trick .. But 40 years late! - quantalume - 01-18-2016, 06:20 PM RE: Useful Programming Trick .. But 40 years late! - Garth Wilson - 01-18-2016, 07:32 PM RE: Useful Programming Trick .. But 40 years late! - Namir - 01-19-2016, 03:20 AM RE: Useful Programming Trick .. But 40 years late! - Katie Wasserman - 01-19-2016, 05:34 AM RE: Useful Programming Trick .. But 40 years late! - Namir - 01-19-2016, 07:29 AM RE: Useful Programming Trick .. But 40 years late! - ttw - 01-21-2016, 08:59 AM RE: Useful Programming Trick .. But 40 years late! - Garth Wilson - 01-21-2016, 09:14 AM RE: Useful Programming Trick .. But 40 years late! - Martin Hepperle - 06-27-2016, 11:33 AM RE: Useful Programming Trick .. But 40 years late! - ttw - 01-21-2016, 10:02 PM RE: Useful Programming Trick .. But 40 years late! - rprosperi - 01-21-2016, 11:45 PM RE: Useful Programming Trick .. But 40 years late! - ttw - 01-22-2016, 01:55 AM

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