TI-86 BASIC Tutorial
Chapter 4: Basic Control
In this chapter you will learn to control basic program flow.
Lbl and Goto: In TI-BASIC, labels and gotos are probably the most important means of of controlling your programs. They allow the program to jump from one place in the code to another. Make a program called "REPEATER", then enter this code:
When you run the program, it will display "Hello". When the program hits Goto A, it jumps to Lbl A. Therefore it displays "Hello" again and again and again... It will never quit. Press [ON], then [F5] to break (stop) the program. This program has no purpose, but as you will see later, these two commands are very important.
If, Then, Else, End: The If, Then, Else, and End commands are also extremely useful. They will perform an action IF a condition is true. Start up the editor and create program "IFFY". Enter this code:
:Disp "Enter a number larger","than 10:"
:Disp "Hey, that was smaller","than 10!"
This program asks the user to enter a number greater than 10. The program now checks to see if A<10. If it is, the program will execute the code between the Then and the Else command. If A>10 it will instead execute the code between the Else and the End commands. The above if command can be read in plain English like this: "IF A is less than 10 THEN display 'Hey, that was smaller than 10!', or ELSE display 'Good!', then END the if command." You can omit the Else command if not needed, for example:
:Disp "A equals:",A
Here, if A<10 the program executes the code between the Then and End commands. If A is NOT <10, the entire If command is skipped. If there is only 1 action to be taken if a condition is true, you can leave out the Then and End commands. For example:
:Disp "A is greater than 45."
This is read: "IF A>45, display 'A is greater than 45.'" And now a word of caution about equal signs in If commands: On the TI-86, a single equal sign (=) is used to set a variable equal to something (For example, A=35 is similar to saying 35->A). In an If command you must use the "is equal to" command (==). For example, this is valid:
And this is NOT valid:
The second, invalid example produces an error.
Pause: The Pause command stops the program until the user hits [ENTER]. Make a new program called "PAUSER" and enter this:
This little program displays "Hello", pauses, then clears the screen after the user presses [ENTER]. The Pause command also includes the option of displaying text like the Disp command. Therefore, the program can be rewritten like this:
This version does the exact same thing but is shorter. It displays "Hello", then pauses. You can also have the Pause command display variables, like this:
This time the program stores 50 to variable A, displays the contents of A (Which, of course, is 50), and then pauses. Once again, the screen is cleared after the pressing of the enter key. One last thing that the Pause command can do is allow the user to scroll left and right to read a long string. Type this in:
:Disp "Use arrows to scroll:"
:Pause "I am truly a very long string! Using the Disp command, I would not fit on one line!"
Run the program and check it out! This can be useful for things like putting your long website URL into a program...
Menu(: The Menu( command is very useful indeed. It creates a menu at the bottom of the screen just like the ones that the calculator's operating system uses. The syntax is Menu(Column#,"Title",LabelToGoto. There are five columns, corresponding with the keys [F1] through [F5]. Here is an example:
:Outpt(4,3,"Select an Option:"
:~Code for "Play"~
:~Code for "About"~
In this program, "Select an Option" is displayed in the middle of the screen. A menu with three options appears at the bottom of the screen: "Play" is in the first column, "About" is in the third, and "Quit" is in the fifth. If the user presses [F1] (The key below the first option, which is "Play"), the program jumps to label P as if there were a Goto command. If the user hits [F3], it goes to label A. [F5] goes to label Q. You can have more than 5 menu options. If there are more than 5, the user simply hits the [MORE] button to see more options.
In the above program, "Beef", "Steak", and "Peas" appear in columns 1, 3, and 5. When the user hits [MORE], "Cheese" appears in column 1 and Quit appears in column 5. Try it out, as this command is very useful.