TI-BASIC (Round Three) (Control Flow: If-Then-End)
Consider the following code:
1->A
If A = 1 Disp "A=1"
Let’s study it a bit. The first line just stores 1 as A. Standard stuff. The second line is freaky, and the third line just displays “A=1″. Standard stuff. Let’s go back to the freaky part. It introduces one of the more powerful commands in TI-BASIC: If. What it says, is that if A equals 1, to do something. The syntax isn’t nearly as remarkable as the code, just If [value][thingy][value]. Thingy can be anything from the ‘Test’ menu. =, <, >, etc.
Note: Due to the constraints of this keyboard, the less-than-or-equal-to sign will be written as <=, and the greater-than-or-equal-to sign will be written as >=. Also, the not-equal-to sign will be written as !=.
If you’re looking for ‘if’, it’s under ‘Ctl’, not ‘I/O’. And this is the first time we’re using ‘Ctl’…Yay! OK, digression over
If’s power comes from the fact that it introduces uncertainty into the program. This means the program can rocket off in a lot more directions, which means: Sweeeeeeet.
Since you handled that chunk of code so beautifully, let’s consider this one too.
Input A If A=1 Disp "A=1" Disp "Where is this?"
I hope you’re wondering if the ‘Where is this?’ line is part of the if statement or not. It’s not. The only command that’s conditional is the one immediately after the if line. Which is really useful for a code snippet like this:
If A=1 Disp "A is 1" If A=2 Disp "A is 2" If A>2 Disp "A is pretty big" If A<1 Disp "A is pretty small" That's a lot cleaner than the following method, which is also legal.
If A=1 Then Disp "A is 1" End If A=2 Then Disp "A is 2" End If A>2 Then Disp "A is pretty big" End If A<1 Then Disp "A is pretty small" End
You’re probably wondering why that’s legal. It’s stinking huge! Which brings us back to our problem–if only lasts for a line. If-Then-End fixes that problem. Then means that all of the following instructions are only to happen if the statement is true, and End means that an end has been put to the conditional instructions. There’s no arguments required for Then or End (an argument is something that has to be put with it to make the syntax legal.) So let’s look at our ‘Guess!’ program again. Last time we’d gotten this far:
Disp "Guess!","","(GPL) 2008", "T. Macdonald"
#Note that going from the outline to the implementation there was a better idea--putting 'Guess!' and the GPL on separate lines. Input "Guess:".G #Oh no! We don't know how to make a random number! #Oh no! We don't know how to say if the number is too big or too small! #(That's why the tutorial's not done. Stay tuned.) And now we do know how to say if a number is too big or too small! So #I'm not copying all the code, we'll pick it up here: Input "Guess:", G If G<R Disp "Too small!" If G>R Disp "Too big!" If G=R Disp "You win!"
Unfortunately, that’s really all we can do with that, as of right now.
Next time we’ll talk about Else.
Assignment: Write a program that will ask the user for his age, and then will classify them as “Kid” (12 and under), “Teenager” (13-19) or “Adult” (20+).

