Every command on the calculator is useful for programming, but some are expressly designed for that purpose. These are roughly divided into three categories: commands to control the flow of a program, commands to manage variables, and commands that provide input and output.
Any program is just a sequence of commands; but usually, it's not enough to just run through them all one by one in order and be done. Changes to this order have to be made: some commands should only be followed in certain situations, others might be repeated several times, still others might be a last resort in case of an error. Control flow commands are those that determine this order.
A common situation is when certain commands depend on a condition being met. The following commands address this situation:
- The If statement simply places a condition on a command or block of commands.
- The Else statement, used with If, provides an alternative branch if the condition is not met.
- The ElseIf statement, also used with If, allows for several mutually exclusive conditions.
Loops allow commands to be repeated over and over. The following types of loops exist:
- Loop..EndLoop blocks just keep repeating forever.
- While..EndWhile blocks repeat as long as a condition is satisfied.
- For..EndFor blocks repeat a fixed number of times.
To deal with these loops, we have a couple auxiliary commands:
If a task has to be done several times inside a program, the code for it can be removed to a subroutine: this routine can then be "called" whenever necessary. Subroutines in TI-Basic are essentially programs of their own. They can be defined inside a program with the Define command, and are of two types:
- Functions (defined with Func..EndFunc) return a value, and can't affect the overall state of the calculator.
- Programs (defined with Prgm..EndPrgm) can do anything, but don't return a value directly.
Two commands exist specifically for use with subroutines.
- Return exits a subroutine, returning to the program that called it.
- Stop exits every program currently running, subroutine or not.
Error catching provides a last resort if an error occurs. A portion of the program, or even the entire program itself, can be put into a Try..Else..EndTry block. The code after Else will only run in case of an error, and has the option to ClrErr — go on as though nothing happened — or PassErr — handle the error normally, with an OS error message.
Another part of programming is managing variables. This means:
- Defining them — with → or Define.
- Deleting them — with DelVar, DelType, DelFold, or NewProb.
- Protecting them — with Lock, Unlock, isLocked(), Archive, Unarchiv, or isArc().
- Dealing with names and folders — with CopyVar, MoveVar, Rename, or NewFold, setFold().
There is also the Local command, which designates certain variables as local to the program they are used in.
Input and Output
Finally, a program has to be able to get input from the user and give some output in response. For games, this usually means using the graphics commands, and reading keys with the getKey() command. There are other alternatives, however.
On the Program I/O screen:
- Input, InputStr, and Prompt read in typed text.
- Disp, Output, and Pause display text.
- ClrIO clears the I/O screen.
- Input here is accomplished with Request and DropDown.
- The Text command, appropriately, displays text. A Title is also handy.
Input and output also refers to interacting with other calculators. There are five commands for the purpose: