"Advanced TI-82 Programming Techniques"

With your friendly neighborhood super guy: Frank Force

Welcome back to another version of everybody's favorite TI-82 programming techniques column. Well it is the only TI-82 programming column, so.

Anyway, the response was huge from last issue! Two letters! OK, so I wrote to myself one time. Keep writing, I love to hearing from you. Email - (*****@voicent.com) Before I start with what I had planed, I should mention the benefits of using Repeat instead of Goto.

Almost every program that I've seen so far uses Goto way too much. Why shouldn't you use it? Three big reasons:
1) It is slower. The Repeat function is much faster (even slightly faster then While).
2) It eats up lots of memory. You've probably noticed that programs that loop many times a second can get memory problems, the best example of this is snake. This is because it uses Goto and not Repeat (at least the version I saw did). Breakout, which uses Repeat, rarely runs out of memory after you get over the initial barrier of the list and matrix.
3) It will make the program slower each time it is accessed. You may not have noticed this. If you play a game that uses Goto too much, and you don't stop, it can get a lot slower. Repeat has this problem too, but it's not as bad.

The only reason you should use Goto is for menus where it works great.

BYPASSING IFS
Now, we're really going to start making programs faster! You're going to start noticing some big differences in your program's speed with these tips. If you have an If that manipulates a variable and that's all, you can use this to make it faster.

:If X = 5
:Z + 1 -> Z

The previous statement can be converted to:

:Z + (X = 5) -> Z

Because if X does equal 5 it would add 1 to Z. If X does not equal 5 it would add 0 to Z. Likewise this would work:

:If X != 5
:Z + 2 -> Z

change to

:Z + 2(X != 5) -> Z

This works because it multiplies the result of X != 5 by 2. I also changed the equal sign to the not equal to sign (!=), to show that this trick will work for any type of logic statement. Including and, not, or, & xor.

:If A = 2 and B != 7
:C - 4 -> C

change this to

:C - 4(A = 2 and B - 7) -> C

Pretty simple stuff. I optimized B != 7 to B - 7 with the trick from last month. I did not optimize the And by multiplying. Most of the tricks from last month won't apply here because you won't always get a 1 from a true statement. I can't explain everywhere that you could or couldn't optimize, but experiment and you will probably figure it out.

THE ANS STATEMENT
Ans is probably the strangest variable that I have ever come across. Fortunately we can use it to out advantage. First I will explain how it works, then I will show how it can be used to increase program speed.

Everybody has probably used the Ans statement outside of a program. Inside a program it works a little different. To store a constant to Ans just type it on a blank line.

:5

That's it. No fuss, no muss, and it's faster then if you stored 5 to a regular variable. All of these statements WILL store a number to Ans:

:3
:5 -> X
:Y = 4
:not A
:Z - 2
:Ans + 1

All of these statements WILL NOT change or store a number to Ans:

:Prompt A
:Disp X
:Disp Ans
:For (B, 1, 100
:IF Z = 3

Now, because Ans works faster then regular variables, we can use it in place of temporary variables. You saw this in the example getkey program last month.

Repeat Ans = 75.5 / 4.5 [Repeats until you press enter.]
Getkey - 25 [Stores in ans]
If abs Ans = 1 [Left (24) or Right (26) - 25.]
Then [So now Left = -1 and Right = 1]
[Run if you pressed Left of Right]

Else
Ans/4.5 - 1 [Up (25) or Down (34). Do the math...]
If abs Ans = 1 [So now Up is -1 and Down is 1]
Then
[Run if you pressed Up or down]

End
End
End

If you're memory conscious, Storing variables to Ans take up less space than the regular way. And it's faster! How could you go wrong? Well actually, you can if you forget that ans gets over written each time a number is stored to a variable. But as long as you keep this in mind, Ans should help make your programs much faster and more efficient.

Bonus section - REVISING CODE
I know, it's ironic that I'm going to tell you about revising code after Breakout and Dungeons & Dragons take up so much memory, but they WERE revised. Because I see so much code that could and should be shorter here are some ways to revise.

Take out * symbols.
:10*A -> B
:10A -> B

The last parenthesis can be removed from any statement.
:ipart (X / 2) -> Z
:ipart (X / 2 -> Z
also
:If (X = 3)(Y = 3)
:If (X = 3)(Y = 3

The last quote can be removed from any statement.
:Disp "GAME OVER"
:Disp "GAME OVER
also
:Output (4,1, "You Win")
:Output (4,1, "You Win

Avoid repeating code, instead use subprograms, and don't forget to use the Ans statement.

Those should help you out. Next time, don't tell me that my games are too big. Last month I said that a colon (:) should not be used to combine lines of code because it takes up space and makes programs hard to read. I have been informed that it does not take up any more space then using enter. I still think that it make's programs hard to read, but it's your choice.

Next month we'll talk about the Get function and subprograms.