Mathematical operators to replace logical operators

From Ninerpedia
Revision as of 17:34, 10 September 2014 by Stephen Shaw (talk | contribs) (New article on TI BASIC logical operators)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

TI Basic Programming:

One omission from TI Basic is the lack of logical operators such as AND and OR used for example in expressions such as IF V<8 AND W>4 THEN 120 ELSE 240. TI Basic programmers instead used mathematical operators to replace logical operators.

Another omission was that such comparisons could only lead to a line transfer instead of a command. TI BASIC did not allow IF V<8 THEN W=4.

These omissions were dealt with in Extended Basic.

However, TI BASIC does have 'relational operators' which will often help you out of this problem.

Value of TRUE and FALSE

The instruction IF X=1 THEN 100 is acted upon by the computer only if the expression (X=1) is TRUE. A TRUE expression is treated by the computer as having a value of -1, while a FALSE expression is treated as having a value of 0.

TRUE value for line transfers

The IF..THEN structure does not require an expression to evaluate to 0 or -1 however, as any value other than 0 is treated as TRUE for line transfer purposes.

You may also use a variable on its own to perform a line transfer:

IF X<>0 THEN 100 will transfer to line 100 if the variable X has any value.

IF X THEN 100 will have exactly the same effect but use less memory.

Replacement for AND

It is possible in TI BASIC to build up a set of expressions in an IF...THEN line, which will simulate OR and AND, and if you are careful, you may go well beyond OR and AND.

Each expression to be evaluated MUST appear in brackets. For example: IF (A=1)+(B=10) THEN 100 may be interpreted as IF A=1 OR B=10 THEN 100.

If both A=1 and B=10, then the sum of the two expressions is -2, (-1 plus -1), and as the result is not zero, the transfer to line 100 will take place.

If only A=1, but B=5, then the sum will be (-1+0) or -1, and the transfer will still take place.

If A=3 and B=5, then the sum is (0+0) or 0, and the transfer to line 100 will not occur.

What we have then is a way of saying IF A=1 OR B=10 THEN 100 (dont type this line in).

Replacement for OR

Using a different mathematical operation, the multiply or *:


IF (A=3)*(B=2) THEN 100

When A=3 and B=2, the calculation is (-1 * -1) or +1.

The result is none zero and the line transfer takes place.

When A=3 and B=1, the calculation is (-1 * 0) or 0

The result is zero so no transfer will occur.

What we now have is a way of saying,in TI Basic: IF A=3 AND B=2 THEN 100.

More complexity possible

Provided you are careful to always know the possible results of the various expressions and mathematical operations, you may build up some very powerful IF...THEN commands, which will save you many lines of tedious programming.

Setting Variables using logical operations

This however is not all you can do with relational expressions. How about trying to program IF A=5 THEN B=6 ELSE B=0 in TI Basic?

B=-6*(A=5) has exactly this result.


If (A=5) then the calculation is B=-6*-1, or B=6.

If A does not equal five, the calculation is B=-6*0, or B=0.

All you need is to keep track of the possible values of the variables you use.

Multiple Variable comparisons

To whet your appetite: IF (X=1)+(Y=1)+(Z=1)=-2 THEN 100

This interesting group of expressions will transfer to line 100 if any two of the three bracketed expressions is true but NOT transfer if zero, one or three variables have a value of 1.

What this fairly short line says to the computer is:

If any two of X Y & Z are equal to one then...

Using < and >

As well as testing for equality you can test for greater than(>) and less than(<) or even "less than or equal to" (<=) and "greater than or equal to" (>=).

By keeping track of the variations in values following comparisons you can build up some powerful and memory saving routines.

String Variables

The same structures can be used with string variables.