TI-86 Basic


Pythagorean Theorem: A2+B2 = C2


What you'll learn:
First of all, Prompt. Here's the syntax:
Prompt variable
Whatever you put as variable will appear on the screen with a question mark after it, waiting for you to type something and press [ENTER]. Input works the same way, but has optional text, like this:
Input "Text:",variable
With both Prompt and Input you can type in numbers. Also, if the graph screen is showing during a program and you just have Input, you can move the pointer around to get coordinates when you press ENTER (good for graphical games). With InpSt you can type in words to save as strings. Then you have getKy, that small, very useful, part of every good game. It checks if a key has been pressed (that's all it does). Before using it you must learn all the other commands on this page. Now you have Lbl, which is just a marker or a placeholder. You shouldn't ever have Lbl without Goto. If you're confused, an example is about to be shown. Goto simply goes to a Lbl.
CodeExplanation
Lbl AMarks the place as A
Input "Age:",AAsks for a number, which is stored as A
InpSt "Name:",BAsks for a name, which is stored as B
Disp A,BDisplays age and name
Goto AStarts over at a place called A
The only way to get out of this program is to press [ON], so it's not very helpful. Keep waiting for the Big One. What if you want something to happen depending on a certain value of A? That's where Conditional Statements come in. Look at the code to see how the If...Then...End helps.

CodeExplanation
Lbl AMarks the place as A
Input "#:",AAsks for a number, which is stored as A
Disp A2Displays A squared
If A==0 or A==1Does the following commands if you happen to put in 1 or 0 for A
ThenOptional if only 1 thing is done, but usually best for syntax.
Disp "A = Aany power.Displays the message, you can substitute any number for 'any power'
Goto BThen it goes to label B, also a way to quit the program
EndEnds the If...Then...End statement and the checking
Goto AStarts over at a place called A
Lbl BStarts a place called B, which is at the end of the program
DelVar(ADeletes the variable A
Notice that even if a command was explained in a previous lesson, I still explain it here, because maybe I used a new form or argument. The TI-85 and TI-86 are the only ones that often need 2 = signs. The code you just looked over (and should understand) is a stand-alone program ...I wouldn't be surprised if something very similar exists on ticalc.org. Make sure you understand everything covered so far before moving on. Next is the If...Then...Else...End statement which does something no matter what, but you have complete control over exactly what happens either way. Look at this example:

CodeExplanation
Lbl AMarks the place as A
getKy→AAny key press is stored in A
If A==22:Goto BGoes to label B if you press [MODE], no Then or End needed for only 1 command
If A==45Does the following commands if you press [CLEAR]
ThenOptional if only 1 thing is done, but usually best for syntax.
ClLCDClears the screen
Disp 45Displays 45
ElseBut if A isn't 45
Disp ADisplays the value of whatever key you pressed
EndEnds the If...Then...Else...End statement and the checking
Goto AStarts over at a place called A
Lbl BStarts a place called B, which is at the end of the program
DelVar(ADeletes the variable A
Don't forget about the special cases with If A=22:Goto B and If getKy>0:getKy→A since that saves lots of bytes. This, too, is a stand-alone program, and can be used to find the getKy value for any key, except [ON]. We finally covered the amazing getKy command. You can look at the Key Map Picture. It didn't have to be stored as A, but if we didn't do that only zeros would be displayed, since it's a very fast acting command. A 0 means no key was pressed during that loop. The Else wasn't needed but you need to know how is used. What's left in this lesson?... The While...End & For...End loop, the weird IS>( and DS<(, then the Pythagorean Theorem program to sum up and effectively use what you just learned. I just thought of a good example covering the last 4 commands, here it is:

CodeExplanation
While getKy=0Continues looping until you press a key
For(X,0,100,5Uses every 5th number from 0 to 100; the 5 is optional
IS>(X,50:Disp "Greater than 50.If X<50, it adds 1 to X each loop until it can display the message
DS>(X,50:Disp "Less than 50.If X<50, it subtracts 1 from X each loop until it can display the message
EndEnds the For...End statement and its looping
EndEnds the While...End statement and its looping
DelVar(XDeletes the variable X
The While...End and For...End don't check anything, they only loop. The For...End increases a value by 1 unless a 3rd argument is present (the step, in the it's 5). If you want the routine to run backwards use a negative step; you can't use 0. The While...End loop runs until a certain condition is met. If you notice the adding and subtracting of the DS>( and IS>( cancel each other out, which keeps the For...End running smoothly. They're very rarely used, so don't worry too much about them. For keycodes for any of your programs use the example given. Now for your last command this lesson, Repeat. Simply look at this example, it shows how Repeat can be used:

CodeExplanation
0→xDefines x to avoid an error
Repeat AContinues looping until you press a key
x+1→xIncreases x each loop
Disp xDisplays x
getKy→AStores any key value to A
EndEnds the Repeat...End statement and its looping
Disp ADisplays A
DelVar(xDeletes the variable x
DelVar(ADeletes the variable A
It's an uncommon command, but useful in keycode dependant programs. An the example it repeats until A>0. Here's what you've probably been waiting for, the Pythagorean Theorem program:

CodeExplanation
FloatLets values have up to 12 decimal points
NormalMake values appear in normal notation
ClLCDClears the screen
Disp "1. A2","2. B2","3. C2Displays 3 lines of options using 1 command, very efficient.
0→AStores A as 0, so there's no error when you press a key.
While A≠92 or A≠93 or A≠94 or A≠45loops unless you press 1, 2, 3, or CLEAR
getKy→AAny key press is stored in A
If A==92 or A==93:Goto CGoes to the label C if you press 1 or 2.
If A==94:Goto AGoes to the label A if you press 3.
If A==45:Goto DGoes to the label D if you press CLEAR.
EndUnless you pressed a key, it loops and checks again
Lbl CStarts the C label
ClLCDClears the screen
If A==92Simply, if you pressed 1
ThenThe the program will
"B2:"→DStores the string "B:" as D
ElseBut if you pressed 2 instead
"A2:"→DStores the string "A:" as D
EndEnds this checking and storing
Input "C2:",CAsks you for C2 and stores it as C
Input D,BAsks you for A2 (if 2 was pressed) or B2 (if 1 was pressed) and stores it as B
Disp C-B,√(C-BDisplays A2 or B2, and either A or B
PausePauses so you can copy the answer
Goto DGoes to the label D to quit
Lbl AStarts the A label
ClLCDClears the screen
Input "A2:",AAsks you for A2 and stores it as A
Input "B2:",BAsks you for B2 and stores it as B
Disp A+B,√(A+BDisplays C2 and C
PausePauses so you can copy the answer
Lbl DStarts the label D
ClLCDClears the screen
DelVar(ADeletes the variable A
DelVar(BDeletes the variable B
DelVar(CDeletes the variable C
DelVar(DDeletes the variable D
Always read though the 'Explanations' to see what's going on. Once you read and understand this Lesson you can call yourself a programmer when applying what you've learned. There's nothing new in the Pythagorean Theorem program except the use of logic and the use of a few arithmatic functions. Make sure you know why it works before continuing. I can't explain this any further, because then you wouldn't learn how to solve the 'Why and How' of logical operations yourself.

This concludes Lesson 2 "Input and Conditional Statements"
On to Lesson 3: Modes, Menus, and Linking

Any Errors need to be reported to William White.