Page 1 of 1

Logic Function

Posted: Mon Feb 05, 2018 12:11 pm
by GeorgeMcGinn
SmartBASIC allows you to write Functions you can use in calculations, like X=Haversine(X,Y)+100.

So I took davey110's problem 3+(x=5) and created a LOGIC function to do what x=5 would do. Since it is a function, it can also be included in a library of standard functions, so I include it here with some additions to it.

I ran his calculation 3+(x=5), making x=5 and it printed 4, the correct answer.

Other versions of BASIC will evaluate (x=5) as True, then return a 1. Then 3+(1) will print as 4.

Here is the LOGIC function that does the same thing. It is basic with only equal and not equal. You can expand this to add other logic checks, like "less than", "greater than", "integer", etc.

As the sample code shows, you can test it in an "IF" statement, or any "DO/UNTIL" or "WHILE/WEND" statements.

You're only limitation, besides what that language can't do, is your imagination and coding skills. As a systems programmer most of my life, I've never met a computer problem I could not successfully code.

This may not look pretty, but when you look at those classic BASIC programs that have this logic test in it, now you should be able to alter the formula slightly and get the desired results.

George.

Code: Select all

X=5
print 3+LOGIC(X,"=",5)

if LOGIC(X,"=",5) then answer=3+LOGIC(X,"=",5)
print "The answer to 3+(x=5) is: "&answer

STOP

DEF LOGIC(A,O$,B) 
    IF O$="="  THEN
       IF A=B THEN RETURN 1 ELSE RETURN 0
    ENDIF
    IF O$="<>" THEN 
       IF A<>B THEN RETURN 1 ELSE RETURN 0
    ENDIF
ENDDEF