# Invert the colors of the graph

I've heard that Lbl and Goto use up memory that they don't give back. I've noticed clearing the RAM on my calculator frees up a lot of space and makes my programs run a lot faster. Any suggestions on how to minimize memory leaks here would be greatly appreciated. (This code does function correctly other than leaking memory).

:-1->B
:Lbl H
:B+1->B
:0->A
:If B=95
:Goto E
:Lbl V
:Pxl-Change(A,B)
:A+1->A
:If A=63
:Goto H
:Goto V
:Lbl E


I've represented the STO→ character, → with -> Also, the graph is 94 pixels wide and 62 pixels tall.

I've heard that Lbl and Goto use up memory that they don't give back

If you replace the following ...

:If B=95
:Goto E
... etc. ...
:Goto V
:Lbl E


... with ...

... etc. ...
:If B<95
:Goto V


... then you have eliminated the final Lbl E as well as its Goto.

I don't think you can do better than that: the remaining two gotos are required: unless you unroll the loops.

• This is assuming that < can be used in an :If statement? – ChrisW Jan 15 '14 at 16:27
• Okay, thanks :) It's been tested, and it works. Only thing is that B<94 has to be B<95 – Timtech Jan 15 '14 at 16:28
• Oh, sorry (removed that comment). And also accepted. – Timtech Jan 15 '14 at 16:35
• @Timtech Thanks for the feedback. I fixed the answer. – ChrisW Jan 15 '14 at 16:38

I think I've mastered loops in TI-BASIC, after reading up some more on the TI-BASIC wiki. Although I haven't tested it (yet), this code should theoretically have the same function:

:-1->A
:While A<62
:A+1->A
:0->B
:While B<94
:B+1->B
:Pxl-Change(A,B)
:End
:End


Note that @ChrisW's answer is still the correct one, as it doesn't introduce actual loops (it just eliminates the Goto E and Lbl E)

• Maybe While doesn't leak in the way that Goto does? – ChrisW Jan 15 '14 at 17:05
• @ChrisW Yeah, I read up on it. Sorry to answer my own question here, but I did find out that While doesn't leak like Lbl and Goto. – Timtech Jan 15 '14 at 21:56
• TI-Basic has For( loops. – M. I. Wright May 24 '15 at 20:29

Your code does something for which the TI-BASIC For loop was intended.

For(A,0,62
For(B,0,94
Pxl-Change(A,B
End
End


This will execute the Pxl-Change for each value of A from 0 to 62 inclusive, and each value of B from 0 to 94 inclusive. If you replace B with the sequence variable n found at [2nd][CATALOG][N], it will be faster because n is stored in a fixed location in memory and is faster to access.

By the way, your original code from January 2014, although it is slow, has no memory leaks. Those only happen when you have a block that should end with an End, but you Goto somewhere else before you reach that End. So

Lbl A
If 1
Then
Goto A
End


will have a memory leak, but not the following.

Lbl A
If 1
Goto A


Note also that all memory used by programs for flow-control purposes is given back to the calculator after the program is finished executing. Therefore, although it's bad practice, it is okay to have a one-time memory leak (for example, leaving off an End if it's the last statement in your program and you will never jump back.