TI-Basic Z80 Programming/Advanced Programming

This section will focus on advanced programming techniques which will help you manage your code better and create advanced coding structures.

Idioms edit

Idioms are pieces of code that are used repeatedly from program to program.

Invisible "Done" edit

the following code will clear the screen and get rid of the Done that follows programs. If the last command that alters the screen (e.g., Disp, ClrHome, etc.) is Output(, then Done will not appear.

ClrHome
Output(1,1,"


A different solution is to set a variable, string or a number at the last line. That value will be shown at the end of the program. So to remove Done, just put an empty string at the end of your program.

ClrHome
"


Booleans edit

Note that boolean values are represented by 1 and 0 where 1 is true and 0 is false. This makes integrating true/false conditionals into equations very convenient.

Never use = tests for boolean values. Instead you can use:

If A

for "If true" and use:

If not(A

for "If not true."

It may not seem like much, but in the end, it will save you a lot of memory.

Use DelVar edit

The power of DelVar often goes unrecognized. The following are all legal code using DelVar:

Delvar ADelVar BDelvar C
DelVar ADisp B
Delvar ADelVar B5→C

As you can see, DelVar is a very efficient way to delete variables, as it can also run another command on the same line.

Despite this, there is a catch:

:If A=1 :Then :DelVar AEnd :End


If A=1, it deletes A and then produces a Syntax error, because End is encountered twice. If A=0, nothing happens and the program exits normally because the End after DelVar A is ignored. This is a design flaw in the TI-Basic interpreter.

Scrollable Lists edit

Scrollable lists can be quickly created by pausing on a list, for example,

Pause L1

However, its not the nicest looking layout, so only use it if you need to create a program really fast.

Pause edit

On the subject of pause, you can pause on variables, strings, and text, which is convenient.

Pause A
Pause Str1
Pause "My name is David Becker"


Movement with getKey edit

Changing the X and Y variables with key presses could be done with four If commands:

:getKey→K :If K=24 :X-1→X :If K=26 :X+1→X :If K=25 :Y-1→Y :If K=34 :Y+1→Y


This is a better way to do it:

:getKey→K :X-(K=24)+(K=26)→X :Y-(K=25)+(K=34)→Y


Ans edit

If you can, don't place values in variables, but store them in Ans. Ans is a much faster way of storing values, and it won't overwrite any other data. The only bad thing is that if you store a value in variable, Ans is set to that value.

10
Disp Ans


For( trick edit

You can end a For( loop prematurely by setting the variable it iterates to the end value:

ClrHome
1→X
Disp 2
Lbl A
X+2→X
1→A
For(Z,3,√(X)) // Here is the For( loop start.
If not(fPart(X/Z))
Then
0→A
√(X)→Z // This is where the variable is set.
End
End // The loop will end here if Z is set to √(X), so the remaining dividers of X are not checked.
If A
Disp X
Goto A


If trick edit

You can have these kind of constructions:

If condition 1
Then
code executed if condition 1 is true
If condition 2
Else
code executed if condition 1 is true or if condition 2 is true after the code block is executed
End

These can save a lot of space in some situations.


Previous: Tips, Tricks and Optimizations
Next: Programming Games
Table of Contents: TI-Basic Z80 Programming