Conditional statements

Conditional statements are how you make a program behave differently based on comparisons. First of all, know that an equation can return true or flase. (ex. 5=5 returns true, 3=5+-3 returns false, sin(x)^2+cos(x)^2=1 returns true even if x is undefined).

A. Booleans

Booleans are the values true and false. We have a couple of commands to combine these: bool AND bool, bool OR bool, bool XOR bool, and NOT bool. NOT inverts the boolean. The following explains the others.

ConditionResultConditionResultConditionResult
true and truetruetrue or truetruetrue xor true false
true and falsefalsetrue or falsetruetrue xor falsetrue
false and truefalsefalse or truetruetrue xor truetrue
false and falsefalsefalse or falsefalsefalse xor falsefalse


B. The IF statement

Suppose we wanted our program to do different things depending on a boolean. We can use IF bool to compare. If the boolean is true, the IF statement will execute the next line of code. Otherwise, the next line is skipped. Now suppose we wanted it to execute a few lines. Well, we add a THEN statement after the IF and add an ENDIF after the block we wanted executed. Ex:

DemoIF()
prgm
Dialog
Title "IF Demo"
Dropdown "Take a guess",{"Answer A","Answer B","Answer C"},A
Enddlog
If A=2
Then
Dialog
Title "Correct"
Text "Good Job"
Enddlog
Endif
Endprgm

Now, what if we wanted it to tell us if out answer was wrong. Well, we could put it in another IF statement:

DemoIF()
prgm
Dialog
Title "IF Demo"
Dropdown "Take a guess",{"Answer A","Answer B","Answer C"},A
Enddlog
If A=2
Then
Dialog
Title "Correct"
Text "Good Job"
Enddlog
Endif
If A<>2
Then
Dialog
Title "Wrong"
Text "Ha ha"
Enddlog
Endif
Endprgm


But that's bulky. Forunately, there's an easier way. The ELSE statement runs if the boolean after the IF is false:

DemoIF()
prgm
Dialog
Title "IF Demo"
Dropdown "Take a guess",{"Answer A","Answer B","Answer C"},A
Enddlog
If A=2
Then
Dialog
Title "Correct"
Text "Good job"
Enddlog
Else
Dialog
Title "Wrong"
Text "Ha ha"
Enddlog
Endif
Endprgm


It's a little shorter, and easier. We can also use WHILE bool to loop until is true. See the GETKEY() section for an example. Up to here, everything is similar to other programming languages. TI-BASIC has another nice tid-bit: TRY statements.

C. TRY Statements

Now, in some cases, our user might enter something that's invalid. Now, we could use IF statements to check the validity of what they enter, but that's time consuming. We can use a TRY statement with what we were going to do anyway. Then, if an error occurs, the program will jump to the ELSE of the TRY. Ex:

DemoTry()
prgm
Dialog
Title "Try demo"
Request "Folder Name",F
Enddlog
Try
Setfold(#F)
Else
Dialog
Title "Can't change folders"
Text "That folder doesn't exist"
Enddlog
Endtry
Endprgm


I'll just tell you that setfold(#F) will change the present folder to the folder with the name that is the value of F. You'll find out why in section IX. So this program tries to set the folder to what you enter, buit if no such folder exists, errors and tells you it can't. If this TRY statement weren't being used, the program would error and quit. When the error occurs the system variable errornum is, so you can customize your error messages using IF statements to check is errornum equal to whatever type of error you are checking for. This is usually more trouble than it's worth, however. TIP: Use TRY statements when linking between calculators to avoid a program crash if the link comes out.

<<Previous - Contents - Next>>