[7.37] Simulate Switch Or Case Statement With Goto And Indirection - Texas Instruments TI-89 Tip List

Graphing calculator
Hide thumbs Also See for TI-89:
Table of Contents

Advertisement

to put 32_°F in the history display, then press [DIAMOND] [2] to run kbdprgm2(). A dialog box shows
the result of 273.15K.
For conversion programs such as this, it would be even more useful if the result is returned to the
history display, as with functions. This is not possible using built-in TI Basic functions, but it can be
done with the copyto_h() ASM program described in tip [7.8]. This version of kbdprgm2() uses
copyto_h() to return the result to the history display:
kbdprgm3()
Prgm
Local t,tk
expr("ans(1)")→t
tmpCnv(t,_°K)→tk
util\copyto_h(tmpcnv("t","tk")
EndPrgm
As before, the temperature is entered in the history display. When [DIAMOND][3] is pressed to run
kbdprgm3(), the result is directly returned to the history display.
(Credit to Andrew)

[7.37] Simulate SWITCH or CASE statement with GOTO and indirection

Some computer languages include a SWITCH or CASE statement which transfers program control to a
label based on the value of an integer variable. For example, some versions of Basic implement this
structure as an ON ... GOTO statement:
ON i GOTO l1,l2,l3
Program control is transferred to l1, l2 or l3, when i=1, 2 or 3, respectively.
TI-Basic does not have this statement built-in, but it can be simulated using goto, indirection and string
manipulation.
First method
As an example, suppose we have a variable i and we want to branch to labels l1, l2 or l3 when i is 1, 2
or 3, respectively. The branch is accomplished with
goto #(mid("l1l2l3",2*i-1,2))
The program labels are specified as a single string, and the mid() function extracts the two label
characters based on the value of i. Note that all the labels must have the same number of characters
(two in this example), and i must be a valid value, or an error will result. The error can be avoided and
handled by using the try...endtry structure.
In general, if each label is n characters long, then the general form of the expression is
goto #(mid( label_string ,n*i-(n-1),n))
For example, if each label has four characters, then the expression is
goto #(mid( label_string ,4*i-3,4))
7 - 36

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Ti-92+

Table of Contents