The Rogue's
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:

:Lbl A
:Disp "Hello"
:Goto A

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:

:Lbl A
:Disp "Enter a number larger","than 10:"
:Input "",A
:ClLCD
:If A<10
:Then
:Disp "Hey, that was smaller","than 10!"
:Goto A
:Else
:Disp "Good!"
:End

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:

:If A<10
:Then
:Disp "A equals:",A
:End

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:

:If A>45
: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:

:If A==10
:Disp A

And this is NOT valid:

:If A=10
:Disp A

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:

:Disp "Hello"
:Pause
:ClLCD

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:

:Pause "Hello"
:ClLCD

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:

:50->A
:Pause A
:ClLCD

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!"
:ClLCD

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:"
:Menu(1,"Play",P,3,"About",A,5,"Quit",Q
:Lbl P
:~Code for "Play"~
:Lbl A
:~Code for "About"~
:Lbl Q
:ClLCD
~Program Quits~

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.

:Menu(1,"Beef",B,3,"Steak",S,5,"Peas",P,6,"Cheese",C,10,"Quit",Q

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.

Practice Program: Quadratic Equation Solver 2.0 - Practice the skills you learned in this chapter



Good, now proceed to Chapter 5: Loops.

Click to Return to This Tutorial's Home Page