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
Post Reply 


Messages In This Thread
Useful Programming Trick .. But 40 years late! - Namir - 01-18-2016 04:31 PM



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