Begining TI-82 BASIC
This tutorial teaches the reader the basics of TI-82 BASIC programming.
|Table of Contents|
|Lesson 1: Getting started|
Lesson 2: Displaying Text
Lesson 3: Variabls and Input
Lesson 4: If...Then...Else statements
Lesson 5: Loops
Lesson 6: Random Numbers and Your first game
Lesson 7: The graphscreen
In order to write a BASIC program on the TI-82, you must first get to the program editor. To get there, press PRGM. Then go to the NEW menu, and press enter. The calculator says "Name=" followed by a flashing cursor. This is where you type the name of the program you will write. Just type whatever you want here for now, and press enter. The screen that appears is the program editor, where you can enter the commands that make up your program. The commands are located in menus that you can get to by pressing the PRGM button. The CTL (which stands for Control) menu contains commands that tell the calculator to do "behind the scenes" tasks, like making decisions or jumping to other parts of a program. The I/O (which stands for Input/Output) menu contains commands that tell the calculator to do things that the user will see happening, like displaying text or allowing the user to enter numbers. Just ignore the EXEC menu for now. To exit the menus, press CLEAR.
To exit the program editor, press 2nd QUIT to go back to the home screen. To edit a program that you have already started, press PRGM, go to the EDIT menu, and choose your program. When you are finally ready to run your program, press PRGM, then select the program's name from the EXEC menu. If you ever need to delete a program, press 2nd MEM, choose Delete..., choose Prgm..., select the program you want to delete, and press enter. (Note: Be very careful with this one! Once you press enter the last time, the program is gone FOREVER!) Use 2nd QUIT to exit back to the home screen when you're done deleting stuff.
O.K., so all you need to know how to do now is actually write the program. We'll start with something simple.
Displaying text on the screen is probably the simplest thing to do on the calculator, but it is also one the most important. If a program doesn't display anything on the screen, the user won't know what's going on!
The Disp command is used to display text. To get to it, press PRGM, go to the I/O menu, and choose option 3. (From now on, I'll use the abbreviations like PRGM:I/O:3 to say which menu option to choose. PRGM:I/O:3 will take you to the Disp command.) To see how this command works, create a new program and enter the following (press ENTER at the end of each line):
Now exit the program editor and run the program you made. You should see something that looks like this:
The first two lines were pretty predictable; they simply printed what was inside the quotes. But notice what happened to the 12345 when we took away the quotes. The number lined up to the right side of the screen. Whenever you use the Disp command without quotes, the number lines up on the right instead of the left. (Note that you can't use letters without quotes. If you do, you'll get weird stuff instead of letters. We'll learn about that in a later lesson.) The fourth line worked just like the first two in that it just printed what was inside the quotes. When you take away the quotes, like in the fifth line, the calculator figures out what 3+3 is, and displays the answer. This is useful for programs that do calculations for the user.
One more note about displaying things on the screen. Notice that when you ran your program, you could still see things you had typed before you ran the program. It is generally good practice to start your program with a ClrHome (PRGM:I/O:8) command, which clears the home screen. To do this, go back into your program (using the PRGM:EDIT menu). Go to the beginning of the first line of the program, and push 2nd INS. Now push ENTER to create a new line before the first. Go to the new line and put in a ClrHome command. When you run the program, notice that the output (what the program spits out) starts at the first line of the screen.
Now you know how to display things on the screen, but to be honest, that alone gets pretty boring after a while. For a program to be interesting, or at least useful, the user has to be able to interact with it. But before you can do that, you have to understand what variables are.
Start a new program and enter the following commands:
When you run the program, you should see this:
O.K., this is what happened here. The first statement clears the home screen. Then comes the statement 3->A. This takes the value 3, and says whenever the program sees the letter A (outside of quotation marks), it means 3. So when the program says Disp A in the third line, it knows to display 3. The next line says to take 3+5, calculate it, and store the result as A. This means that A does not mean 3 anymore; it means 3+5, or 8, which is displayed in the next line. The next line is a little strange - A*2->A. What the program sees this, it takes the current value of A, 8, and multiplies it by 2, making 16. Then it stores that answer as A, making A equal to 16. You can use any combination of expressions and other variables when defining a variable. The only thing you can't do is make a variable equal a word or group of letters (called a string). In other words, the statement "VARIABLE"->A will give you an error. And by the way, a variable does not always have to be called A. It can be any letter A-Z, or theta, so you can have up to 27 different variables at once.
So now that you know what variables are, what can you do with them? They're not very useful if you have to define them within the program itself. The main use for variables is letting the user define the variable, and then doing stuff with what the user defines. You can do this with the Input (PRGM:I/O:1) statement. Enter the following program:
When you run it, you will see a question mark and a flashing cursor. This means that the calculator is waiting for a response from the user. Enter any number and press enter. The program then stores the number as N, and displays whatever you entered. For example:
The only problem with this is that if the person using your program doesn't know what your program does, he or she won't know what to do when the cursor pops up. To fix this, enter teh following:
Now, instead of a question mark and a cursor, the program will say ENTER NUMBER: followed by a cursor. Let's do one more example of this to show more about how variables and Input work together. Enter the following program:
This program lets the user enter two numbers, finds the average of them, and displays it. This shows why you might want more than one variable in a program. These past two lessons have dealt with the two major concepts of a program: letting the user input information, and giving the user information. Now, it's time to do some interesting things with the information.
At some time or another, most programs have to make a decision of some sort, doing one action under a certain condition, or another action under a different condition. The way you can accomplish this is with an If (PRGM:CTL:1) statement. Try this program:
O.K., so it's a dumb program that doesn't do much. The point is to notice what happens when you put in an age bigger than 17, and what happens if you put in a smaller one. Here is what you should get when you run the program using different values for A:
See what the If statement on the third line does? If the condition A>17 is true, then the line after the If statement executes; otherwise the program ignores it. What if you want more than one thing to happen if the condition is true, though? In this case, you would use a Then (PRGM:CTL:2) statement. Put the commands you want to occur between Then and End (PRGM:CTL:7), and those statements will only happen if the If statement is true. Look at this example:
Now the program will display YOU CAN VOTE and OR JOIN THE ARMY if the age is 18 or over. Now, what if you wanted to display something is the person is 18 or over, and display something different if they're younger than 18? You do this with an Else (PRGM:CTL:3) statement, which is used like this:
If A is bigger than 17, the program displays YOU CAN VOTE; otherwise it says YOU CAN'T VOTE. Notice that you still have to use Then and End if you use Else, even if you only want to have one command occur.
The last thing to know about If statements is all the comparisons you can make. Below are some charts that list all the different comparisons you can make. Notice that you can make more than one comparison in a single If statement. For example, if you wanted to test to see if someone's age was between 18 and 21, you would say If A>17 and A<21... to do it. The words you use to joins comparisons are under 2nd TEST:LOGIC.
Loops are very useful statements that let you repeat a set of commands over and over without having to type them many times. For instance, say you wanted to make a program that displayed the numbers 1-100. You could do it like this:
and so on, but that would take a LONG time to enter. To avoid this, you can use what is called a For (PRGM:CTL:4) loop. For loops let you repeat any number of statements a specified number of times. Here's what our counting program would look like with a For loop:
The second line looks pretty strange, doesn't it? Here's what it does. First, it takes a variable (F in this case), and makes it equal to 1, which is the second thing in the parentheses. Then the program continues until it hits an End statement. At this point, the program adds one to F (kind of like saying F+1->F). If F is smaller than the third number (100), the program loops back to the For statement and performs the commands inside the loop again. Otherwise, the loop ends. As another example, let's say you wanted to display all the even number from 1-100. Here's what you do:
Notice what changed in the second line. The first number changed from 1 to 2, because we want to display even numbers, and 1 is odd. Also, the last number changed from 1 to 2. This means that instead of adding 1 to F every time the loop ends, the program adds 2.Well, this is fine if you know how many times you want the loop to happen, but what if you don't know how many times to repeat the loop? For example, how would you make a program that asks a number to input a number over and over until he enters a 0? You can do this with a While (PRGM:CTL:5) loop. Look at this example:
When the program starts, the user is asked to enter 20, and the program stores whtever the user entered as N. Then, if the user did not enter 20, the commands inside the While loop happen. The While loop repeats as long as the condition in the While statement is true. Once N=20, making the While loop FALSE, the While loop ends.
One thing to be careful about with While loops is what is called an infinite loop. Check this out:
This loop will never end!! N starts as being 10, and every time the loop occurs, N gets bigger. N will always be greater than 0, so the loop will go on forever. To stop the program when something like this happens, press the ON button. This kind of error is actually pretty common, so do your best to avoid it.
Up until this point, the program examples have been pretty simple, and pretty useless as well (except for teaching). Now we're going to put all these things together to make a game. The game I had in mind was a random number guessing game, where the calculator will "think" of a number between 1 and 100, and the user will try to guess. The only thing left to learn before we make this game is how to tell the calculator to make a random number. Here is the command to do it:
int is located at MATH:NUM:4 and rand is at MATH:PRB:1. Do not actually type in what is in italics. High means the highest number the random number can be (in our game it will be 100). Low is lowest number (1 in our game). Variable is whatever variable you want to represent the number. So to make a random number between 1 and 100, you would enter:
O.K., so let's get to the game. Try to follow in your head what the program does, line by line, before you read the paragraph after the listing. This will help you to understand how the calculator interprets programs.
The first line clears the screen. Then, the calculator makes a random number between 1 and 100 and stores it as N. Then it stores 0 as G (this is so G and N are sure to be unequal before the While loop starts). The calculator tells the user to enter a guess, and it stores the guess as G. If the user guesses too high (G>N), the program says "TOO HIGH!" If the user guesses too low (G Congratulations! You've just made your first game using all the commands covered in the previous lessons. Now you know all the basic commands necessary to make any text-based game. But while many (probably most) of your programs will be text-based, what if you wanted graphics in your programs? Well, that's where we go from here...
Congratulations! You've just made your first game using all the commands covered in the previous lessons. Now you know all the basic commands necessary to make any text-based game. But while many (probably most) of your programs will be text-based, what if you wanted graphics in your programs? Well, that's where we go from here...
While text games are okay, graphics make everything better. Unfortunately, you can't make graphics on the home screen (at least, not very well). To do good graphics, you need to use the graph screen. The graph screen is where you see all of your functions graph (obviously). The graph screen is 94 pixels across by 62 pixels down. To display things on the graph screen, you have to tell the calculator what coordinate you want it display on. Here's a list and a brief description of what most of the commands for the graph screen do:
|ClrDraw||The equivalent of ClrHome for the graphscreen. It clears the graphscreen||2nd, PRGM, 1|
|Line(||Draws a line between the point X1,Y1 and X2,Y2||2nd, PRGM, 2|
|Horizontal||Draws a horizontal line at the Y-coordinate specified||2nd, PRGM, 3|
|Vertical||Draws a vertical line at the X-coordinate specified||2nd, PRGM, 4|
|Text(||Displays starting Y pixels down, X pixels to the right||2nd, PRGM, 0|
|Pxl-On/Off/Change||Turns on, off, or toogles the pixel that is y pixels down and X pixels to the right||2nd, PRGM, >, 4/5/6|
|Pxl-Test(||Represents a number value of 1 if the specified pixel is on, 0 if it's off||2nd, PRGM, >, 7|
As a general rule, I start each program where I'm going use the graph screen like this:
Those last four lines make it so that if you use Line, Horizontal, or anything that use points on the coordinate plane, the numbers will be the same as if it used pixels down and to the right. The only difference is that you use a negative number for pixels down. Now, these settings could make people mad at their calculator after the game is over, because it messes with their viewing window for graphs. To fix this, I use these lines at the end:
And that's about it! Now you can program just about anything at all on the calculator! Soon I'll make an advanced TI-82 programming lesson which will tell how to do animation, use lists and matrices, and other cool stuff that can help your programs be even better. But for now, just try stuff out; that's the best way to learn BASIC. Experiment with commands; you can't do anything that will hurt your calculator. And last (but definitely not least!), tell me what you thought of the lesson! If you have any questions or comments, or need help with program, PLEASE feel free to e-mail me at email@example.com. I'll be glad to answer any question. Have fun programming!