If you wish to return to the top of this page,
click the little blue arrow thingys that look like this
.
Chapter Five: Conditions
Chapter Six: Branching
Chapter Seven: Conditional Loops
Chapter Eight: More I/O
Chapters 5-8 Review
Back to tutorial index | Part One | Part Three
Conditions are necessary in many programs because they direct the flow of the program depending on if certain things happen.
THE FAMOUS If/Then... Else BLOCK
This is probably the most famous command ever and it exists in some form in practically every programming language on earth, from the high-level C++ to the lowly assembly language (cp for all you ASMmers out there!)
Basically what it does is it says, if this happens, do this. If not, do this. The menu locations for If, Then, Else, and End are PRGM \ CTL \ 1 , 2 , 3 , 7 respectively.
It is used like this:
:If condition
:Then
:commands
:End
or:
:If condition
:Then
:commands
:Else
:commands
:End
You might be asking, what is a condition? I don't know how to put it in words but here are some examples:
A=8, B<20, C>D, etc. = , < , >, <, >, and the not-equal-to sign can all be found in the TEST menu by pressing [2nd] [TEST]. (TEST is above MATH.)
So now that you know a bit about conditions, here's an example to clear everything up. It's a program that tells whether a number is even or odd.
:ClrHome
:Input "ENTER AN INTEGER",N ----- If you forget what
you learned in algebra, an integer is a whole number.
:If fPart(N)/=0 ----- fPart? /=? fPart(
is MATH \ NUM \ 4. It takes the decimal
portion of a number. /= is the not-equal-to symbol. Don't type a /
and an =.
:Then ----- This
if/then block checks to make
sure the answer is an integer, and if it isn't it lops off the
decimal.
:int(N)->N ----- int( is
MATH \ NUM \ 5. It chops off a decimal
from a number.
:End
:If fPart(N/2)=0 ----- Divide N by 2. If
there is no decimal, N is even (it's divisible by 2).
:Disp "EVEN."
:Else ----- If there is a
decimal, N is odd.
:Disp "ODD."
:End
:Stop
Here's what the program should look like on your calculator: (NOTE: I expanded it so you can see the whole program.)
MORE POWERFUL CONDITIONS
All right, maybe X>8 and Q=1 are OK, but what if you need to use two conditions in one If statement? That's easy, use and. Consider this example:
If X=1 and Y=1
Then
do stuff
End
Get it? X must equal 1 and y must equal 1 in order for this statement to be true. and can be found at TEST \ LOGIC \ 1.
Cool, but what if you want the if statement to be satisfied if one thing or another thing happens? Use the or operator (TEST \ LOGIC \ 2).
Example:
If X<0 or X>6
Then
do stuff
End
If X is less than 0 or greater than 6 the calculator will do stuff. The calc will also do stuff if both conditions are true.
XOR AND NOT
If you've been exploring your calculator, you might have noticed 2 other commands in the TEST \ LOGIC menu.
These commands are less frequently used. xor is almost like or, but in order for the calculator to do stuff only one of the conditions must be true. This means that if both conditions are true the calculator will not do stuff.
For those of
you who know your binary and hex, you'd think that not( would return the one's
complement of the number. But not on the TI-83.
not( returns 0 if the number
is not zero. To me it seems pointless. I don't see any use for this
command so forget about it unless you really need it.
Now you should know something or other about if/then/else blocks.
Right now, don't your programs seem a bit, well, linear? Well now you're gonna learn 2 commands that'll have you jumping around all over your program.
LABELS
Before you can jump to a specific point in your program, you must designate the "jump points", or the places where you want the program to go. They are called labels, and they are indicated with the Lbl command. (PRGM \ CTL \ 9).
After the
Lbl command, you must
designate the name of the label. It can be one or two letters or
numbers and theta 0. Good labels could be
0, 99, X, or AC.
Now a label won't do anything by itself. It just sits there. If you want to jumlp to a label, use...
THE WORLD-FAMOUS AND HIGHLY UNDERRATED Goto STATEMENT
Yes, that's "go to". Goto has been one of the most controversial commands in all of programming. For years, many people have shunned Goto, and told their followers to do the same! Although it is a very inefficient command, and sometimes there are substitutes, it can still be very useful.
It is used very much like Lbl. A label name, preferrably corresponding to a label somewhere in your program, must follow it.
A word of advice: As we go on, you will learn alternatives to Goto. Don't overuse it, or you'll wind up with "spaghetti code".
Oh, by the way, its menu location is PRGM \ CTL \ 0.
Now it's time for an example I couldn't think of a good short one so I modified one from the TI-83 manual. It takes a number and squares it. If the number is greater than 100 it exits.
:ClrHome
:Lbl 99
:Input A
:If A>100 ----- Note how there is
no Then command. You don't have
to use Then if there's only one
command you want to be executed.
:Goto X
:Disp A2
:Goto 99
:Lbl X
:Disp "GOODBYE."
:Stop
See how that works? If the number is greater than 100, it jumps to the label called X, which stops the program. If it's not greater than 100, it goes back to 99 after it displays the square of the number.
In the next chapter you'll learn about some alternatives to Goto.
If you can't figure out what a loop is, it's just a part of a program that keeps repeating over and over. Take the following code for example:
:Lbl 5
do stuff
:Goto 5
This program simply repeats forever (or until someone presses the [ON] key), but it is very inefficient. There are other ways, called conditional loops.
While AND Repeat
These commands are a cross between an If statement and that Goto loop you saw before.
Let's start off with While. It is a loop that continues to loop as long as a specified condition is true.
It is used like this:
:While condition
stuff-to-do-while-condition-is-true
:End
While will take a condition much like any other If statement, (A=0, X>3, etc.) While's menu location is PRGM \ CTL \ 5, and End's is PRGM \ CTL \ 7.
The condition is tested when While is encountered. (this is important)
I'm no good at making up examples so here's one straight from the TI-83 manual.
:0->I
:0->J
:While I<6
:J+1->J
:I+1->I
:End
:Disp "J=",J
This program simply increases J and I by 1 as long as I is less than 6.
Repeat is a lot like While, but it repeats a group of commands until the condition is true. It also tests the condition when End is encountered, unlike While. The way it is used is no different. Its menu location is PRGM \ CTL \ 6.
So if you were to re-write the above example using Repeat, you could change While I<6 to Repeat I>6.
If you wanted to make a While or Repeat loop that loops forever, use the following conditions:
While 1
or
Repeat 0
INCREMENTING LOOPS
Let's say you need a loop that only goes through a certain number of times. That's easy, with the For( command. Its menu location is PRGM \ CTL \ 4.
It is used like this:
:For(variable,start,end,increment)
stuff-to-do-while-end-hasn't-been-exceeded
:End
variable is a variable that will be incremented (made bigger). It is the counter variable, and it holds the current number that the For( loop is on. start is the number that the counter will start at, and end is the number the counter will end at. increment is optional . It tells the counter how much to increase each time through. If an increment is not specified, the counter increases by 1 each time through. You can make the counter count backwards if start is bigger than end and increment is negative.
Here are a couple examples to demonstrate the uses of For( :
:ClrHome
:For(A,0,10,2)
:Disp A
:End
Output:
:ClrHome
:For(X,1,6)
:Disp "A FOR LOOP"
:End
Output:
As you can see, the top program displays the status of the counter each time through. This can be used in many ways. The bottom program does one task multiple times. Instead of writing Disp "A FOR LOOP" 6 times, this way is much more efficient.
NESTED LOOPS
"Nesting" loops means putting loops inside loops. You can do this with While, Repeat, For(, and even If-Then-Else. You can also mix and match, like putting a For( loop in an If-Then block or a While loop inside a Repeat loop. Just try and imagine those little wooden dolls that you would open up, and there'd be another one inside, and you'd open that one up, and there'd be another one inside that one, and so on.
More ways to use input and output.
"PRESS ENTER TO CONTINUE"
Ever wanted your program to just stop, and wait for the user to press [ENTER]? Well now you can. How? Use the Pause command (PRGM \ CTL \ 8). That's it.
It's used like this:
Pause
If you want, you can specify a variable or value after Pause, and it will display it, and it can be scrolled. This is good if you want to display something that goes off the screen, because the user can pause, and scroll the value to see all of it.
Example:
:Disp "PRESS ENTER"
:Pause
:ClrHome
:Stop
Output( - TEXT ANYWHERE YOU WANT IT!
With the Output( command (PRGM \ I/O \ 6), you can display text anywhere you want on the screen. You simply specify where you want to put the text, and the text or value you want to display.
It's used like this: Output(row,column,"text") or Output(row,column,value)
row must be between 1 and 8. column must be between 1 and 16. They are the coordinates of the first character of the text (or value).
The row coordinates go top to bottom, and the column coordinates go left to right. Here's how the screen is set up:

Also, if you Output( something to the last row (row 8) the screen will not scroll up. This could be used if you want to display 8 lines of text simultaneously.
Here's a little example: It displays an "X" at the coordinates you specify.
:ClrHome
:0->C
:0->R
:Repeat R>1 and R<16
:Input "ROW:",R
:End
:iPart(R)->R
:Repeat C>1 and C<8
:Input "COLUMN:",R
:End
:iPart(C)->C
:ClrHome
:Output(R,C,"X"
:Pause
:ClrHome
:Stop
Output:

You can also display multiple things on one line. Consider this line of code: Output(3,3,A," PLUS ",B," IS ",C
WELCOME TO THE TI-83 RESTAURANT, HERE IS YOUR Menu(
Sick and tired of making menus with all those cumbersome Disp commands, then an Input command for the person to enter their choice in? If you've ever done this, you're probably going to shoot yourself when you find out that there is an incredibly easier and user-friendlier way (or you'll shoot me for not telling you earlier!)
The Menu( command is used for making menus identical to the ones in the calculator's operating system. Its menu location is PRGM \ CTL \ C.
It is used like this: Menu("title","option1",label1,"option2",label2...
The menu can have as many options as you want. title is the title of the menu, option1, option2, etc... are the options you want to be displayed in your menu, and label1, label2, etc... are the corresponding labels you want the program to jump to depending on what choice is picked.
Here's an example:
The command
Menu("MENU","BURGER: 2.50",B,"FRIES: 1.00",F,"SODA: 0.75",S,"I'LL STARVE",X
would make a
menu that would look like this:
So for example, if someone chose item #1 (burger), the program would jump to Lbl B. And if you're trying to find the apostrophe it's at ANGLE ([2nd] [MATRX]) \ 2.
Well now you know enough BASIC to program a text adventure game! Here's a small start.
:ClrHome
:Output(4,3,"ADVENTURE!" ----- The exclamation
point can be found at MATH \
PRB \ 4.
:Output(8,1,"PRESS ENTER"
:Pause
:ClrHome
:Disp "YOU ARE IN YOUR"
:Disp "LIVING ROOM."
:Output(8,1,"PRESS ENTER"
:Menu("WHERE DO YOU GO?","THE KITCHEN",1,"THE
BASEMENT",2,"UPSTAIRS",3,"QUIT",X
:Lbl 1
:ClrHome
:Disp "YOU ARE NOW IN"
:Disp "THE KITCHEN."
...
:Lbl 2
:ClrHome
:Disp "YOU GO DOWN TO"
:Disp "THE BASEMENT,"
:Disp "BUT YOU SLIP ON"
:Disp "THE STAIRS AND"
:Disp "DIE."
:Output(8,1,"GAME OVER"
:Pause
:ClrHome
:Stop
:Lbl 3
:ClrHome
:Disp "YOU WALK UP TO"
:Disp "THE 2ND FLOOR."
...
:Lbl X
:Disp "YOU"
:Disp " COMMIT"
:Disp " SUICIDE."
Output(8,1,GAME OVER"
:Pause
:ClrHome
:Stop
To further extend this adventure, add menus to the "..." parts and branch out. If you want random events to happen, the next chapter will talk about random number generation.
And don't forget, use alternatives to Goto whenever possible.