Programming the TI
IF-THEN statements are conditional transfer commands that make it seem as if computers can think. IF a specified condition is true, THEN the program skips to a certain line number elsewhere in the program; otherwise, the program simply continues to the next line as usual. TI BASIC also allows an ELSE statement as part of IF-THEN. It takes this form:
IF condition THEN linel ELSE line2
IF the condition is true, THEN the computer goes to linel, or ELSE the computer goes to line2. If the optional ELSE is omitted, control merely passes to the following line. Here's a common example:
200 IF SCORE = 10 THEN 900 210 PRINT SCORE
This statement says that if the value of the variable SCORE is equal to 10, then the program should continue at line 900. Otherwise, the program continues to the next line and prints the score.
You can use the other relational operators to define conditions in IF-THEN statements, too:
300 IF A<B THEN 700 400 IF X>Y THEN 200 ELSE 580 500 IF J<>8 THEN 800
In each case, the computer evaluates the condition—the expression between the words IF and THEN. If the expression is true, it has the value of —1. If the expression is false, it has the value of zero. Therefore, a statement such as this is valid:
320 IF A THEN 400
This doesn't look like the more common relational examples, but it implies that if A is equal to — 1, then the program goes to line 400.
The condition may look more complex. If you keep in mind that true is — 1 and false is zero, you can usually follow the logic. An example is:
150 IF (A = B) + C THEN 200
The part within the parentheses (A=B) is evaluated first. If A equals B, then the expression is — 1 (true); if A does not equal B, the expression is zero (false). This value is then added to the value for C. If the result is — 1, the condition is true and control passes to line 200.
Most other versions of BASIC allow the use of AND and OR in IF-THEN expressions, TI BASIC does not, but we can translate. Again, keep in mind that — 1 indicates true.
Suppose we want to test the conditions A=B and C=D. If both are true (IF A=B AND C = D), then we want the program to continue at line 700. Here's one way to do this:
IF (A = B)+(C = D)=-2 THEN 700
If both conditions are true, each will yield — 1 values, so the total will be —2.
Here's an equivalent way to make this test:
IF -(A = B)*(C=D) THEN 700
Notice that —1 times —1 is +1, so the negative sign in front converts the whole expression to —1 for true.
The word OR is used when one condition OR the other condition is true, but not both:
IF (X<Y) OR (X>Z) THEN 300
This can be translated to TI BASIC like this:
IF (X<Y) + (X>Z) THEN 300
Program control transfers to line 300 only if the expression evaluates to — 1. This happens if only one of the conditions in parentheses is true (and thus — 1) and the other is false (equal to zero).
Even more complex IF-THEN statements are possible by considering different combinations of + and * in evaluating conditions. Suppose after a CALL KEY statement the user may press either ENTER or any of the number keys. Here's the easiest way to set up the logic:
200 CALL KEY(0,K,S) 210 IF K = 13 THEN 500 220 IF K<48 THEN 200 230 IF K>57 THEN 200
Or you can combine the IF statements like this:
210 IF (K<>l3)+(K<48)+(K>57) THEN 200
The sample program this month is a simple drill for beginning algebra students who are learning to add signed numbers. This program illustrates the use of several kinds of IF-THEN statements.
Lines 200 and 230 show two ways to check the length of the numbers to see if a randomly chosen number is negative. If necessary, a plus sign is added to the number.
Lines 280 and 300 determine the answer depending on the value of SUM.
If the answer is zero, line 360 skips the procedure for choosing the plus or minus sign in the answer. If the student needs to choose the sign, line 420 makes sure he or she presses either the plus sign or the minus sign. All other keys are ignored. Line 490 then receives the number keys pressed.
Line 530 checks the student's answer and branches appropriately. Line 590 waits for the student to press the ENTER key before continuing.
If you wish to save typing effort, you can obtain a copy of "Adding Signed Numbers" by sending a blank cassette or disk, a stamped, self-addressed mailer, and $3 to:
P.O. Box 1502
Cedar City, UT 84720
Adding Signed Numbers
100 REH ADDING SIGNED NUMBERS 110 CALL CLEAR120 PRINT "ADDING SIGNED NUMBERS"::: 130 SCORE=0 140 FOR PROB=1 TO 10 150 T$="" 160 RANDOMIZE 170 A=INT(19*RND)-9 160 B=INT(19*RND)-9 190 A$=STR$(A) 200 IF LEN(A$)=2 THEN 220 210 A$="+"&A$ 220 B$=STR$(B) 230 IF LEN(B)>1 THEN 250 240 B$=" + "&(B$) 250 PRINT "ADD" 260 SUM=A+B 270 S$=STR$(SUM) 280 IF SUM<>0 THEN 300 290 S$=" "&S$ 300 IF SUM<=0 THEN 320 310 S$=" + "&S$ 320 TA=8-LEN(S) 330 PRINT :TAB(4);A$ 340 PRINT :TAB(4);B$ 350 PRINT TAB(3);"---"::: 360 IF SUM=0 THEN 450 370 CALL KEY(0,K,S) 380 CALL HCHAR(23,TA,45) 390 CALL HCHAR(23,TA,32) 400 CALL HCHAR(23,TA,43) 410 CALL HCHAR(23,TA,32) 420 IF (K<>43) + (K<>45)=-2 THEN 370 430 CALL HCHAR(23,TA,K) 440 T$=CHR$(K) 450 FOR J=1 TO LEN(S$)-1 460 CALL KEY(0,K,S) 470 CALL HCHAR(23,TA+J,63) 480 CALL HCHAR(23,TA+J,32) 490 IF (K<48)+(K>57)THEN 460 500 CALL HCHAR(23,TA+J,K) 510 T$=T$&CHR$(K) 520 NEXT J 530 IF SUM<>VAL (T$) THEN 5 60 540 PRINT : : "CORRECT!" 550 SCORE=SCORE+1 560 PRINT :"THE SUM IS "; S$ 570 PRINT :"PRESS <ENTER>. " 580 CALL KEY(0,K,S) 590 IF K<>13 THEN 580 600 CALL CLEAR 610 NEXT PROB 620 PRINT "OUT OF 10 PROBLEMS," 630 PRINT :"YOUR SCORE IS";SCORE: : : 640 END