Site hosted by Angelfire.com: Build your free website today!
Back to Main

Conditions & Loops



IF Statements



:If X=1:then
:1->Y
:End

This is the way an IF statement looks like in TI-BASIC. If condition is true (X=1), assign the value of 1 to Y. If X is not equal to one, "1->Y" will be skipped, since the condition is false.

The ":End" is to terminate the IF statement. Without this the interpreter will consider everything thing as a condition until it encounters an ":End"

:If X=1:Then
:1->Y
:Else
:2->Y
:End

In this case, if X is not equal to 1, then 2 will be stored into Y rather than nothing happening.

:If X=1 and P=6:Then
:1->Y
:Else
:2->Y
:End

In this case, 1 will only be stored into Y if and only if X=1 and P=6.
:If X=1 or P=6:Then
:1->Y
:Else
:2->Y
:End

This is very similar to the AND, but it will assign 1 to Y if X=1 or P=6. As long as one is true, it will assign 1 to Y.

WHILE Loops



:1->X
:While X=1
:Disp"X will always"
:Disp"have a value"
:Disp"of one!"
:Disp"unless stated"
:Disp"otherwise!"
:Pause
:End
In this case , the loop has a condition of X=1. While this is true, the loop will continue to function. The "End" marks the end of the loop. It this is reached and the condition is still true, the loop will restart, until the condition is false. In the example above, it will never be false. The pause is used just so you are not flooded with text :P

:1->X
:While X=1
:Disp"X will always"
:Disp"have a value"
:Disp"of one!"
:Disp"unless stated"
:Disp"otherwise!"
:1+X->X
:Pause
:End
:Disp"That's all folks"

In this case, the loop displays the text, pauses (waits for user to press enter) and then adds one to X (1+X->X). Because of this , X is now 2. The loop then encounters the "end" and is then forced to restart , and re-check the condition. x is no longer equal to one, so the loop is ignored, and you see a message saying: "That's all folks". Keep in mind that if the X had not changed, you would of never seen this message, as the loop would of went on forever. NOTE: AND and OR statement can also be used with while loops.

FOR loops



:For(A,1,10,1)
:Disp A
:End
:Disp"Have a nice day"

The way to read the for loop in the example is:
For "A" which starts with the value of 1, finishes with a value of 10, and increments by 1, display A.
Heres how the for loop thinks. "A" starts at one, and so it will display the value of "A" which is one. Once it encounters the "end", the loop re-starts , and checks the condition: is it 10 yet? if no, run loop again. "A" now has a value of 2 (because it's the second time the loop runs), and so it displays "A", which is 2. It continues like that until it A=9, where it will then run the loop ONE MORE TIME, in which "A" will equal 10, 10 will be displayed, and when the loop checks the condition again, "A" will have reached its intended value, so the loop is now ignored, and you see the message, "Have a nice day.".

:For(A,1,10,-1)
:Disp A
:End
:Disp"Have a nice day"

In this example, will "A" ever become 10? The answer is no. All you will see in this case, it the value of "A" flooding down your screen (since it never pauses) getting farther and farther from 10. Depressing isn't it? In fact , this is like when dogs chase their tails, they'll just never catch it, no matter how hard they try. (Assuming the dog actually has a normal spine, a normal sized tail, and etc)


Back to TI-Basic Page

Go Back to Input/Output Basics