[7.50] Modify Loop Control Variables Within The Loop - Texas Instruments TI-89 Tip List

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

Advertisement

:Here is an expression
:x^2+2
:"Let's try a string with quotes"
:
General comments:
For the variable name, you can specify a variable in the current folder or you can give the full path. For
TxtWrite(), if the folder you specify does not exist, you will be prompted to create it.
Any input errors (bad argument type, etc.) are shown in the status line.
TxtRead() is a C program, so you will need IPR, as well as h220xtsr if you have a HW2 calculator. See
tip [12.2] for details.

[7.50] Modify loop control variables within the loop

If you are familiar with other programming languages but not TI Basic, you may not be aware that you
can modify all of the control variables of a For ... EndFor loop, within the loop block. While this is a
dubious practice in terms of code readability and maintenance, it can sometimes result in smaller code.
For example, this loop will never terminate, because the index i never reaches the terminal value n:
1→n
For i,1,n
i+1→n
EndFor
You can change the index step size within the loop:
10→n
1→step
For i,1,n,step
if i=4
2→step
EndFor
For this example, i steps through 1, 2, 3, 4, 6, 8, 10.
The example below is a more realistic demonstration. This segment of code processes a list of
expressions e to return a list of the individual terms of the expression. Initially, list e has only one
element, but it grows as terms are separated, and shrinks as they are removed to the output list o.
while dim(e)≠0
for i,1,dim(e)
e[i]→ex
part(ex)→px
augment(left(e,i-1),right(e,dim(e)-i))→e
if px=0 or px=1 then
augment(o,{ex})→o
elseif px=2 then
part(ex,0)→px0
if px0="+" then
augment(e,{part(ex,1),part(ex,2)})→e
elseif px0="-" then
augment(e,{part(ex,1),⁻part(ex,2)})→e
© Process each element of 'e'
© 'e' reduced by one element
© 'e' increased by two elements here ...
© ... and here
7 - 64

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Ti-92+

Table of Contents