CHAPTER THREE :
HOW TO USE TI BASIC
PRACTICAL PROGRAM WRITING
Before you switch your console on to write a program, or even
gather a large pile of paper to work on, sit and think about
your proposed program.
Work out what your program is to do, and try to split it into
small blocks of tasks to be accomplished. Then you can write the
coding for each block, and check it to ensure you have made no
mistakes, before moving to the next block.
It is much easier to write a small program that works well
than to write in one go a 15k program : it is improbable that it
will work first time, and you will be faced with a lot of
Some experienced programmers can just sit at their console
and input a new program,but certainly at first, you should write
your proposed program down on paper. Check the flow of the
program before you input it: can any variable or input reach a
value which would cause the computer problems? If so, is an
'error trap' required, or does the program need rethinking?
The Basic language is in some ways similar to English: the
same task can be accomplished in several ways, but some ways are
more efficient than others. A method which works well in one
program may be inappropriate in another, therefore it is not
possible to give any more than the most general guidelines.
A digital stopwatch can be an advantage when you are trying
to find the quickest way of doing something. Usually a single
process is too fast to time, but place it in a loop.
(A loop is a part of the program which is repeated several
times, until a particular condition is met. In the following FOR
... NEXT loop the condition is met when the variable I reaches a
value of 1000).
FOR I=1 TO 1000
and it is possible to obtain a reasonably accurate time to
compare with other ways of doing the same thing- for instance,
to place a single character at one screen position you may also
CALL VCHAR(3,4,45) - try substitution in the above loop and
see the difference in execution time.
In general the 99/4A is slow at reading DATA and at scrolling
the screen (PRINT), but there are occasions when these are the
best commands to use.
It is faster for instance to PRINT 24 lines than to use CALL
HCHAR 768 times (the number of characters on the screen)!
Your program idea may need to be amended to meet with the
demands of the computer, and you should never be afraid of
completely scrapping your work and starting again: often new
inspiration can lead to a far more efficient program.
Editing and Debugging
Before developing some useful routines, a word about editing and
debugging : no matter how good a typist you are, even entering a
short program will need the use of these facilities.
EDITING is what you do when you change a program line -
perhaps only one character in the line.
A number of computers use 'screen editing', where you move the
cursor around the screen until it is placed where you want to
make your alteration.
The TI99/4A uses a 'line editor' for programs (some modules
also use a screen editor,eg TI-WRITER). To use a line editor,
you first select the line number you wish to amend, place it on
the bottom of the screen, and move the cursor along the LINE
until you hit the place to be amended.
To bring the line you want on screen, key in the line number,
then hold FCTN down and press key E or X. Your line will appear
with the cursor at the beginning of the line.
Use FCTN and keys S and D to move the cursor over the line
without deleting or altering anything.
If you wish merely to alter the line, typing over it may be
sufficient, but the 99/4A also allows you to delete and insert
To DELETE a character,place the cursor over it (FCTN plus S
or D) and then press FCTN and key 1. This will delete the
character the cursor is on, and everything following will move
one space to the left.
To INSERT text, place the cursor after the last character you
wish to leave untouched, using FCTN and S & D, then press FCTN
and key 2. Now anything you type will force everything after the
insert point one character to the right,and your inserted text
will appear. The cursor position also moves to the right as you
type. You leave INSERT mode by pressing FCTN and S or D, or
When your line is correct, press ENTER to enter the new line
into memory, and leave EDIT mode. However, if you wish to amend
also the line before or after the line just finished, you may
move directly to that by pressing FCTN and E or X.
The maximum line length in TI Basic is four screen lines, but
the computer is often capable of taking a longer line, as the
absolute restriction is on the length of the line in BYTES of
memory used, not the number of characters on the screen.
You may use the edit function to insert 'overlong' lines as
Type in the last part of the required line first. Press ENTER.
With the line on screen, press FCTN and key 2, then key in
the first part of your line,from the beginning. The part you
first entered is pushed to the right.
Now return the line to the screen using the edit mode: type in
the line number and then press FCTN and key X.
It is possible to overfill a line this way, and you will
receive an error message if you do. However you will usually be
able to go to an extra half screen line, and in some cases you
may be able to squeeze two extra lines in!
The advantage of putting as much as possible in a line is
that by using less program lines you save a little memory. In
general, lines with a lot of numbers in will be difficult to
expand in this fashion, but lines with a lot of text or commands
can usually be considerably extended.
When the line is fully entered,press ENTER. Then LIST the
line just to make sure it is all right.
You do not have to INSERT at the beginning of the line, but
you may find it easier to do so. Give the computer time to move
all the characters to the right when using INSERT. Keep your
eyes on the screen.
A brief word of warning: in TI BASIC, your program uses the
same area of memory as the values of variables, separated by a
'marker' in memory. As a program runs, the free memory is
continually filling. Unwanted values are only purged when the
memory is full, resulting in short pauses in program operation.
If the variable area of memory is almost full when you stop
to edit a program, inserting extra material MAY result in the
permanent loss of the marker which marks the limit of the actual
program. This will cause irreversible damage to your program and
may prevent the LIST function from operating correctly, or cause
a system lockout when you try to run the program.
Therefore try to avoid running the program and then editing
it! Before you edit, take a copy of the program to tape - this
has the added benefit of apparently clearing the garbage, and
not only gives you a security copy but actually prevents the
This problem will not be apparent if you use extended basic
plus the 32k ram expansion, as variables then occupy a different
sector of memory.
Having entered your program, you type in RUN, and instead of
the program running, you receive an error message. DEBUGGING is
A BUG is quite simply an error in the program, either a
mistype or an error in your use of Basic.
The computer does check the lines you enter, but will only
spot such things as using only a single bracket ( or quotation
When you RUN your program, the computer first goes through
your program and sets aside memory for each variable and sub
program that you have used. During this 'prescan' further errors
may be spotted and an error message printed on the screen.
Typical errors spotted at this time are incorrect use of arrays
(trying to use DIM after the variable has been used) or a
mismatched number of FORs and NEXTs.
Most errors will only produce an error message as the
computer finds them when your program is actually running - for
instance trying to GOTO a non existant line number, or trying to
RETURN when there is no outstanding GOSUB.
The error messages generated by the TI99/4A are well
described in the manual, and will usually indicate a line number
in which the computer has met something it cannot cope with.
Unfortunately, the actual error may not be in the line stated
in the error message.
For instance, BAD VALUE IN 100 may refer to:
100 CALL HCHAR(ROW,COL,42)
The program line is correct. The error message has appeared
because one or both of the variables ROW and COL have a value
which is out of range.
(Using CALL HCHAR the ROW and COLUMN values passed to the sub
program must be in the ranges 1 to 24 and 1 to 32 respectively.
If you go outside this range the computer will halt with an
To see what the values are, when the error message appears,
just type in PRINT ROW;COL then press ENTER.
The two values on screen will be the current values of ROW
and COL, which have caused the problem.
The job is then to review the program to see how the
variables obtained that value, and see what changes need to be
This ability to check variable values after an error message
is very valuable. Note that once you amend a program line all
variables will be reset to zero.
Frequent causes of problem bugs are DATA statements, with a
comma too many or too few.The ability to check variable values
is useful here:
An array is being filled from a DATA statement, eg
FOR I=1 TO 5
An error message will be generated by the above, as 'A' is
not a number. When the error message appears it is possible to
This will provide the clues needed to lead to the erroneous
DATA line - which may be several hundred program lines away!
In such cases you need to have a good idea of what values
should be found in connection with each variable, and you may
need to spend some time working through the program.
If you cannot fathom why a program is not working as it
should merely by reading the LIST, the TI99/4A also has a TRACE
Key in TRACE and then RUN. The line numbers will be listed on
screen as the program progresses, and you can watch for an
unexpected line transfer as the computer moves from one line to
the next. Using TRACE will disrupt any screen display. Switch
TRACE off by keying in UNTRACE.
To find out what the variable values are at a particular
point in the program, you can insert PRINT statements in the
program, or instruct the program to BREAK by adding a line in
the appropriate place with the instruction: BREAK.
Then when the program stops you may enter PRINT VAR etc.
To continue the program enter CON (and press ENTER)
Remember to remove the BREAK line when you no longer need it.
As your programs become longer, so it becomes more difficult
to spot the errors, but finding and removing errors is a very
good (if time consuming) way of learning how to use your
TI99/4A. With time and experience you will learn to quickly spot
the easy bugs and to tackle the harder ones in a logical
fashion. Read your manual as often as necessary,especially the
section on ERROR MESSAGES.Often the answer to a difficult bug
is there just waiting for you to read it.
Just as with English, your ability to use BASIC increases
with use. Examine as many 99/4A programs as you can and look to
i)WHAT each part of the program does
ii)HOW it does it and
iii)WHY it does it.