<?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%3AFor</id>
	<title>TI-BASIC:For - 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%3AFor"/>
	<link rel="alternate" type="text/html" href="http://learn.cemetech.net/index.php?title=TI-BASIC:For&amp;action=history"/>
	<updated>2026-06-16T07:58:57Z</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:For&amp;diff=504&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:For&amp;diff=504&amp;oldid=prev"/>
		<updated>2016-02-24T18:04:43Z</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;{{Template:TI-BASIC:Command&lt;br /&gt;
|picture=FOR_ANIMATED.gif&lt;br /&gt;
|summary=Executes some commands many times, with a variable increasing from &amp;#039;&amp;#039;start&amp;#039;&amp;#039; to &amp;#039;&amp;#039;end&amp;#039;&amp;#039; by &amp;#039;&amp;#039;step&amp;#039;&amp;#039;, with the default value &amp;#039;&amp;#039;step&amp;#039;&amp;#039;=1.&lt;br /&gt;
|syntax=For(&amp;#039;&amp;#039;variable&amp;#039;&amp;#039;,&amp;#039;&amp;#039;start&amp;#039;&amp;#039;,&amp;#039;&amp;#039;end&amp;#039;&amp;#039;[,&amp;#039;&amp;#039;step&amp;#039;&amp;#039;])&lt;br /&gt;
&amp;#039;&amp;#039;statement(s)&amp;#039;&amp;#039;&lt;br /&gt;
End&lt;br /&gt;
|location=While editing a program press:&lt;br /&gt;
# PRGM to enter the PRGM menu&lt;br /&gt;
# 4 to choose For(, or use arrows&lt;br /&gt;
# 7 to choose End, or use arrows&lt;br /&gt;
|compatibility=TI-83/84/+/SE&lt;br /&gt;
|size=1 byte&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
A For( loop is generally used to do something a specific number of times or to go through each one of a bunch of things (such as elements of a list, or the pixels of your screen). Of all the loops, it&amp;#039;s the most complicated. Its syntax:&lt;br /&gt;
&lt;br /&gt;
 For(variable,start,end[,step]&lt;br /&gt;
 statement(s)&lt;br /&gt;
 End&lt;br /&gt;
&lt;br /&gt;
What the loop does:&lt;br /&gt;
# Stores &amp;#039;&amp;#039;start&amp;#039;&amp;#039; to &amp;#039;&amp;#039;variable&amp;#039;&amp;#039;.&lt;br /&gt;
# If &amp;#039;&amp;#039;variable&amp;#039;&amp;#039; is greater than &amp;#039;&amp;#039;end&amp;#039;&amp;#039; (or less than, if &amp;#039;&amp;#039;step&amp;#039;&amp;#039; is negative), then the For( loop ends immediately.&lt;br /&gt;
# Runs the statement(s).&lt;br /&gt;
# Adds &amp;#039;&amp;#039;step&amp;#039;&amp;#039; to &amp;#039;&amp;#039;variable&amp;#039;&amp;#039; and returns to Step 2.&lt;br /&gt;
&lt;br /&gt;
If no value for &amp;#039;&amp;#039;step&amp;#039;&amp;#039; is given, &amp;#039;&amp;#039;step&amp;#039;&amp;#039; is assumed to be 1.&lt;br /&gt;
&lt;br /&gt;
In other words: &amp;#039;&amp;#039;&amp;#039;a For( loop repeats its contents once for every value of &amp;#039;&amp;#039;variable&amp;#039;&amp;#039; between &amp;#039;&amp;#039;start&amp;#039;&amp;#039; and &amp;#039;&amp;#039;end&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This is perhaps best explained with an example. The following code will display the numbers 1 to 10, in order:&lt;br /&gt;
&lt;br /&gt;
 :For(A,1,10)&lt;br /&gt;
 :Disp A&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
Now, all of this could be done with a [[TI-BASIC:Repeat|Repeat]] or [[TI-BASIC:While|While]] command and some manipulation, except that this is faster because it&amp;#039;s a single command. Still, why have a separate command for something that seems so specific and arbitrary? Well, it&amp;#039;s because For( has so many uses!&lt;br /&gt;
&lt;br /&gt;
* Do something to each element of a list, matrix, or string.&lt;br /&gt;
* Draw several similar objects on the graph screen.&lt;br /&gt;
* Create [[TI-BASIC:Animation|animations]].&lt;br /&gt;
* Easily add the possibility of levels to many games.&lt;br /&gt;
* Any number of other things...&lt;br /&gt;
&lt;br /&gt;
An advanced note: each time the program enters a For( loop, the calculator uses 43 bytes of memory to keep track of this. This memory is given back to you as soon as the program reaches End. This isn&amp;#039;t really a problem unless you&amp;#039;re low on RAM, or have a lot of nested For( statements. However, if you use [[TI-BASIC:Goto|Goto]] to jump out of a For( loop, you lose those bytes for as long as the program is running—and if you keep doing this, you might easily run out of memory, resulting in [[TI-BASIC:Errors#memory|ERR:MEMORY]].&lt;br /&gt;
&lt;br /&gt;
= Advanced Uses =&lt;br /&gt;
&lt;br /&gt;
Sometimes you want to exit out of a For( loop when it hasn&amp;#039;t finished. You can do this by storing something at least equal to the &amp;#039;&amp;#039;end&amp;#039;&amp;#039; value to the variable you used in the For( loop. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 :For(A,1,100)&lt;br /&gt;
 &amp;lt;some code&amp;gt;&lt;br /&gt;
 :If &amp;lt;condition for exiting out&amp;gt;&lt;br /&gt;
 :100→A&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
------&lt;br /&gt;
&lt;br /&gt;
For( can also be used to create a delay:&lt;br /&gt;
&lt;br /&gt;
 //delays for about 0.5 second (83+) or 0.2 second (83+SE/84+/SE/CSE)&lt;br /&gt;
 :For(A,1,200)&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
If X is &amp;#039;&amp;#039;end&amp;#039;&amp;#039;, the delay will be about X/1000 seconds for the TI-83/83+, and X/400 for other calculators.&lt;br /&gt;
&lt;br /&gt;
Unlike delays that use [[TI-BASIC:Rand|rand]], a For( loop delay can execute an animation or other code during the delay.&lt;br /&gt;
&lt;br /&gt;
------&lt;br /&gt;
&lt;br /&gt;
For( loops can be nested to execute code once for every combination of values of several variables. For example:&lt;br /&gt;
&lt;br /&gt;
 :For(A,1,50)&lt;br /&gt;
 :For(B,1,50)&lt;br /&gt;
 :(some code)&lt;br /&gt;
 :End&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
This will run (some code) 2500 times—once for every combination of a value of A from 1 to 50 and a value of B from 1 to 50.&lt;br /&gt;
&lt;br /&gt;
There&amp;#039;s a standard way to exclude repetitions if the order of the variables doesn&amp;#039;t matter (for example, if A=30, B=40 is the same situation as A=40, B=30 in the example above). In this case, the beginning of the loop should be changed to:&lt;br /&gt;
&lt;br /&gt;
 :For(A,1,50)&lt;br /&gt;
 :For(B,1,A)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
------&lt;br /&gt;
&lt;br /&gt;
On the CSE, a list index can be used as the variable in a For( loop. When this is done, the loop will operate and exit normally, but the list will not be affected. For instance, this program&lt;br /&gt;
&lt;br /&gt;
 :{1,2,3→L₁&lt;br /&gt;
 :For(L₁(1),2,5&lt;br /&gt;
 :Disp &amp;quot;X&lt;br /&gt;
 :End&lt;br /&gt;
 :Disp L₁&lt;br /&gt;
&lt;br /&gt;
will output:&lt;br /&gt;
&lt;br /&gt;
 X&lt;br /&gt;
 X&lt;br /&gt;
 X&lt;br /&gt;
 X&lt;br /&gt;
   {1,2,3}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Optimization =&lt;br /&gt;
&lt;br /&gt;
The [[TI-BASIC:Seq_List|seq(]] command, or simple math, can often be used in place of the For( command when dealing with lists. For example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 :For(A,1,dim(L1&lt;br /&gt;
 :cos(A)→L1(A&lt;br /&gt;
 :End&lt;br /&gt;
 //can be&lt;br /&gt;
 :seq(cos(A),A,1,dim(L1→L1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
and &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 :For(A,1,dim(L1&lt;br /&gt;
 :1+L1(A→L1(A&lt;br /&gt;
 :End&lt;br /&gt;
 //can be&lt;br /&gt;
 :1+L1→L1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One rather strange optimization when using For( loops is actually leaving on the ending parenthesis of the For( loop in certain cases. If you don&amp;#039;t do this, the following cases will be processed &amp;#039;&amp;#039;&amp;#039;much&amp;#039;&amp;#039;&amp;#039; slower when they are the &amp;#039;&amp;#039;&amp;#039;first&amp;#039;&amp;#039;&amp;#039; line of code in the loop:&lt;br /&gt;
* [[TI-BASIC:IS&amp;gt;(|IS&amp;gt;(]] and [[TI-BASIC:DS&amp;lt;(|DS&amp;lt;(]] (no matter if the following command is skipped or not).&lt;br /&gt;
* A lone [[TI-BASIC:If|If]] without an accompanying Then, but &amp;#039;&amp;#039;&amp;#039;only&amp;#039;&amp;#039;&amp;#039; when the condition is false (If with a true condition is unchanged).&lt;br /&gt;
&lt;br /&gt;
If the condition of the [[TI-BASIC:If|If]] command can be false (as in most actual cases), you should add a closing parenthesis because the difference is so great.&lt;br /&gt;
&lt;br /&gt;
An example use of this optimization:&lt;br /&gt;
&lt;br /&gt;
 :For(I,1,1200&lt;br /&gt;
 :If 0&lt;br /&gt;
 :1&lt;br /&gt;
 :End&lt;br /&gt;
 &lt;br /&gt;
 //should be&lt;br /&gt;
 :For(I,1,1200)&lt;br /&gt;
 :If 0&lt;br /&gt;
 :1&lt;br /&gt;
 :End&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Command Timings =&lt;br /&gt;
&lt;br /&gt;
Using a For( loop when it fits your purpose is much faster than adapting a While or Repeat loop to do so. Conclusion: For( loops are good!&lt;br /&gt;
&lt;br /&gt;
= Error Conditions =&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;[[TI-BASIC:Errors#increment|ERR:INCREMENT]]&amp;#039;&amp;#039;&amp;#039; is thrown if the increment of the For( loop is 0.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;[[TI-BASIC:Errors#invalid|ERR:INVALID]]&amp;#039;&amp;#039;&amp;#039; occurs if this statement is used outside a program.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;[[TI-BASIC:Errors#undefined|ERR:UNDEFINED]]&amp;#039;&amp;#039;&amp;#039; is thrown if you DelVar the loop variable while inside the loop.&lt;br /&gt;
&lt;br /&gt;
= Related Commands =&lt;br /&gt;
&lt;br /&gt;
* [[TI-BASIC:Repeat|Repeat]]&lt;br /&gt;
* [[TI-BASIC:While|While]]&lt;br /&gt;
* [[TI-BASIC:If|If]][[Category:TI-BASIC]]&lt;br /&gt;
[[Category:TIBD]]&lt;/div&gt;</summary>
		<author><name>Maintenance script</name></author>
	</entry>
</feed>