Conditionals in Ti-BASIC are slightly different than other programming language's conditionals. The first thing you need to understand is that an IF condition tests a number. So:
:IF 0 This won't show up :IF 1 This will show up :IF 2 This will show up
When the IF condition tests the 0 number it returns false, so the code underneath it won't be displayed. Now if the IF comes across a 1, it returns true and executes the code that follows. Now you might be asking yourself "And what about the IF 2?" well in every single programming language the 0 number is equivalent to false and all numbers but 0 are equivalent to true. So when you use an IF condition you are testing if a number is 0 or not 0, if it is false or not false (true) and execute (or not) the instructions that follow.
But what about this:
:1->A :IF A=0 This won't show up :IF A=1 This will show up :IF A=2 This won't show up
Well in this case you may think the IF statement isn't testing a number but a condition. Well you are wrong. Look at the following piece of code:
:1=1 1 :1=4 0
If you type the 1=1 in the main calculator screen you get a 1,meaning that it is true. If you type 1=4 you get a 0, it is false. So, what we can conclude is very simple: conditions return a number whether they are true or false. When comparing two numbers (or variables) the calculator will see if it is true or false and send a number: 0 if it is false; 1 if it is true; So as you can see, the calculator works a IF condition by calculating the Something = Something condition, return a number and if it 1, execute the following code, if it is 0, it doesn't.
Now it makes sense doesn't it? That's why we talked about IF 0 and IF 1 in the begging. So this:
:1->A :IF 1 Do something
Is the same as this:
:IF 1 Do something
Of course the first one can be false if you change the variable's value, but in this case it is all the same thing.
Cool examples that use conditions
1. The best example of the use of conditions is the collision code located here: Collision. It uses conditions to know if the place the character is supposed to go is "walkable" or not.
2. Another rather cool example is the area calculator. You know that an area can't be negative so the program can't return a negative area. Take a look at the next example:
:PROMPT A,B :A*B->C :IF A>0 AND B>0:THEN :DISP C :ELSE :DISP 0
Looks good doesn't it? Well but it isn't. It is true that it correctly displays only positive areas but there's a simpler way of doing it:
:PROMPT A,B :A*B*(A>0 AND B>0)->C :DISP C
In this case, with only 3 lines of code we do the same thing as above and we save memory. Of course this may seem stupid because it only saves a few bits, but in a bigger game production the bit turn into bytes and then Kilo Bytes and the game becomes very big and unusable. Lets just go through the code: The PROMPT asks the user to give some input. Then It calculates A*B and multiplies it be the result of the condition (A>0 AND B>0). So if both are greater then 0, then the condition returns 1 and C becomes A*B. If they aren't the condition returns 0 and C becomes 0. So they are no negative values!!
3. What if you want to know if a certain pixel of the screen is on? Lets see:
:45->X :55->Y :IF pxl-test(Y,X) Do something
Using the pxl-test( command and a IF statement we get the job done!! The pxl-test( returns a number (1 or 0) whether the pixel indicated between brackets is on or off.
Conditions are very handy to test variables and stuff, but have to be used wisely. A very common mistake when using IF conditions is forgetting the THEN after the IF and writing more than one line. See the next example:
:0->A :1->B :IF A≠B :DISP A :DISP B :END
WRONG... Here the THEN command is missing so the calculator would return an error like: ERR: SYNTAX, because there was an extra END. Here's the correct way:
:0->A :1->B :IF A≠B:THEN :DISP A :DISP B :END
And there you have it...
|<< Logic Operations||Table of Contents||Loops Optimization >>|