<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://learn.cemetech.net/index.php?action=history&amp;feed=atom&amp;title=TI-BASIC%3AOptimize_Loops</id>
	<title>TI-BASIC:Optimize Loops - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://learn.cemetech.net/index.php?action=history&amp;feed=atom&amp;title=TI-BASIC%3AOptimize_Loops"/>
	<link rel="alternate" type="text/html" href="http://learn.cemetech.net/index.php?title=TI-BASIC:Optimize_Loops&amp;action=history"/>
	<updated>2026-04-29T20:34:02Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.3</generator>
	<entry>
		<id>http://learn.cemetech.net/index.php?title=TI-BASIC:Optimize_Loops&amp;diff=1226&amp;oldid=prev</id>
		<title>Maintenance script: Initial automated import</title>
		<link rel="alternate" type="text/html" href="http://learn.cemetech.net/index.php?title=TI-BASIC:Optimize_Loops&amp;diff=1226&amp;oldid=prev"/>
		<updated>2016-02-24T18:41:06Z</updated>

		<summary type="html">&lt;p&gt;Initial automated import&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;When using [[TI-BASIC:While|loops]] you want to make them as compact as possible. This starts with moving invariant code outside the loops. You only want loops to contain expressions whose values change within the loops. If something only happens once, it should be outside the loop.&lt;br /&gt;
&lt;br /&gt;
 :For(X,1,5&lt;br /&gt;
 :5→Y&lt;br /&gt;
 :Disp X&lt;br /&gt;
 :End&lt;br /&gt;
 can be&lt;br /&gt;
 :5→Y&lt;br /&gt;
 :For(X,1,5&lt;br /&gt;
 :Disp X&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
You also want to minimize the calculations inside loops. This not only includes cutting down on the number of storages, but how often variables are used and what they are used for. This can increase the size, however.&lt;br /&gt;
&lt;br /&gt;
 :For(X,1,10&lt;br /&gt;
 :A+length(Str1→A&lt;br /&gt;
 :End&lt;br /&gt;
 can be&lt;br /&gt;
 :length(Str1→B&lt;br /&gt;
 :For(X,1,10&lt;br /&gt;
 :A+B→A&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
Another way to minimize calculations inside loops is to use constant increments. This makes the loop faster, but it also makes it larger.&lt;br /&gt;
&lt;br /&gt;
 :For(X,0,10&lt;br /&gt;
 :Disp 10X&lt;br /&gt;
 :End&lt;br /&gt;
 can be&lt;br /&gt;
 :For(X,0,100,10&lt;br /&gt;
 :Disp X&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
You should combine two or more loops that are in close proximity if they use the same number of iterations and don&amp;#039;t affect each other. Combining loops may take some ingenuity.&lt;br /&gt;
&lt;br /&gt;
 :For(X,1,10&lt;br /&gt;
 :B+X→B&lt;br /&gt;
 :End&lt;br /&gt;
 :For(Y,1,10&lt;br /&gt;
 :A+A/Y→A&lt;br /&gt;
 :End&lt;br /&gt;
 can be&lt;br /&gt;
 :For(X,1,10&lt;br /&gt;
 :B+X→B&lt;br /&gt;
 :A+A/X→A&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
Loop unrolling reduces the number of times you check the condition in a loop, with two or more of the same statements being executed for each iteration. If the loop is small enough, you can even unroll the whole loop. This will usually increase the size but also make it faster.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 :5→dim(L1&lt;br /&gt;
 :For(X,1,5&lt;br /&gt;
 :2A→L1(X&lt;br /&gt;
 :End&lt;br /&gt;
 can be&lt;br /&gt;
 :5→dim(L1&lt;br /&gt;
 :2A→L1(1&lt;br /&gt;
 :2A→L1(2&lt;br /&gt;
 :2A→L1(3&lt;br /&gt;
 :2A→L1(4&lt;br /&gt;
 :2A→L1(5&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[TI-BASIC:For|For(]] loops are best used when you know how many times the loop will be executed. Because the fourth argument is optional (one is the default), you should always try to leave it off.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 :For(X,1,8,1&lt;br /&gt;
 :End&lt;br /&gt;
 can be&lt;br /&gt;
 :For(X,1,8&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can sometimes rewrite For( loops and the commands inside them so you can remove the fourth argument.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 :For(X,8,0,-1&lt;br /&gt;
 :Disp X&lt;br /&gt;
 :End&lt;br /&gt;
 can be&lt;br /&gt;
 :For(X,0,8&lt;br /&gt;
 :Disp 8-X&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have an [[TI-BASIC:If|If]] conditional around the outside of a For( loop, you should see if there is a way to combine it with the For( loop using Boolean logic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 :If A&amp;gt;10:Then&lt;br /&gt;
 :For(X,1,50&lt;br /&gt;
 :End:End&lt;br /&gt;
 can be&lt;br /&gt;
 :For(X,1,50(A&amp;gt;10&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One of the common uses of [[TI-BASIC:For|For(]] loops is to slow programs down. Instead of For( loops, you should use [[TI-BASIC:Rand|rand(]]# or If dim(rand(#. Both of these create lists of random numbers, with a larger number meaning a larger delay; the second one preserves the [[TI-BASIC:Ans|Ans]] variable as well.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 :For(X,1,75&lt;br /&gt;
 :End&lt;br /&gt;
 can be&lt;br /&gt;
 :rand(25&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This method generally works well for small delays, but it is better to use For( loops for large delays. This is because the rand(# technique is limited by the RAM storage availability, and has a maximum delay of 999 (being a list variable).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 :rand(200&lt;br /&gt;
 can be&lt;br /&gt;
 :For(X,1,600&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[TI-BASIC:Repeat|Repeat]] loops will loop until the expression is true, and [[TI-BASIC:While|While]] loops will loop while the expression is true. Repeat loops are tested at the end of the loop which means they will be executed at least once. This allows you to not always have to set the variables in the expressions, which is the case with While loops. If the expression in a While loop is false before it is tested, the loop will be skipped over. This is sometimes desired if the expression fits that format.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 :DelVar A&lt;br /&gt;
 :While not(A&lt;br /&gt;
 :getKey→A&lt;br /&gt;
 :End&lt;br /&gt;
 can be&lt;br /&gt;
 :Repeat A&lt;br /&gt;
 :getKey→A&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you need a loop that loops forever (i.e., an infinite loop), use Repeat 0 or While 1 instead of Goto/Lbl.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 :Lbl A&lt;br /&gt;
 :Disp &amp;quot;Hello&lt;br /&gt;
 :Goto A&lt;br /&gt;
 can be&lt;br /&gt;
 :Repeat 0&lt;br /&gt;
 :Disp &amp;quot;Hello&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Goto/Lbl loops should be used sparingly. When [[TI-BASIC:Goto|Goto]] is encountered, it notes the [[TI-BASIC:Lbl|Lbl]] and proceeds to search for it from top to bottom in the code. This can really be slow if the Lbl is deep within the program. It also has the tendency to make your code harder to follow and maintain. And, if you use a Goto to exit a loop or a conditional that uses an [[TI-BASIC:End|End]] command, it can lead to [[TI-BASIC:Memory_Leaks|Memory_Leaks]] (causing your program to crash).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 :Repeat 0&lt;br /&gt;
 :getKey→B&lt;br /&gt;
 :If B&lt;br /&gt;
 :Goto A&lt;br /&gt;
 :End&lt;br /&gt;
 :Lbl A&lt;br /&gt;
 can be&lt;br /&gt;
 :Repeat B&lt;br /&gt;
 :getKey→B&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When all a For( loop does is store expressions to a list, you can replace it with a [[TI-BASIC:Seq_List|seq(]] (sequence) command. The sequence command can also be used with other variables.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 :5→dim(L1&lt;br /&gt;
 :For(X,1,5&lt;br /&gt;
 :2A→L1(X&lt;br /&gt;
 :End&lt;br /&gt;
 can be&lt;br /&gt;
 :seq(2A,X,1,5→L1[[Category:TI-BASIC]]&lt;br /&gt;
[[Category:TIBD]]&lt;/div&gt;</summary>
		<author><name>Maintenance script</name></author>
	</entry>
</feed>