Executes some commands many times, with a variable increasing from start to end by step, with the default value step=1.
For(variable,start,end[,step])
statement(s)
End
While editing a program press:
- PRGM to enter the PRGM menu
- 4 to choose For(, or use arrows
- 7 to choose End, or use arrows
TI-83/84/+/SE
1 byte
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's the most complicated. Its syntax:
For(variable,start,end[,step]
statement(s)
End
What the loop does:
- Stores start to variable.
- If variable is greater than end (or less than, if step is negative), then the For( loop ends immediately.
- Runs the statement(s).
- Adds step to variable and returns to Step 2.
If no value for step is given, step is assumed to be 1.
In other words: a For( loop repeats its contents once for every value of variable between start and end.
This is perhaps best explained with an example. The following code will display the numbers 1 to 10, in order:
:For(A,1,10)
:Disp A
:End
Now, all of this could be done with a Repeat or While command and some manipulation, except that this is faster because it's a single command. Still, why have a separate command for something that seems so specific and arbitrary? Well, it's because For( has so many uses!
- Do something to each element of a list, matrix, or string.
- Draw several similar objects on the graph screen.
- Create animations.
- Easily add the possibility of levels to many games.
- Any number of other things…
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't really a problem unless you're low on RAM, or have a lot of nested For( statements. However, if you use 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 ERR:MEMORY.
Advanced Uses
Sometimes you want to exit out of a For( loop when it hasn't finished. You can do this by storing something at least equal to the end value to the variable you used in the For( loop. For example:
:For(A,1,100)
<some code>
:If <condition for exiting out>
:100→A
:End
For( can also be used to create a delay in the program for some time:
:For(A,1,200)
:End
Change the 200 to a higher or lower number to create a longer or shorter delay.
Actually the rand command can be used to achieve a delay, but at a smaller size. However, only the For( command can be used if you want to do something, such as a simple animation, during the delay.
You can combine two or more For( loops to run a block of statements once for every permutation of several variables. For example:
:For(A,1,50)
:For(B,1,50)
:(some code)
:End
:End
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.
There's a standard way to exclude repetitions if the order of the variables doesn'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:
:For(A,1,50)
:For(B,1,A)
Optimization
The seq( command, or simple math, can often be used in place of the For( command when dealing with lists. For example:
:For(A,1,dim(L1
:A→L1(A
:End
can be
:seq(A,A,1,dim(L1→L1
and
:For(A,1,dim(L1
:1+L1(A→L1(A
:End
can be
:1+L1→L1
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't do this, the following cases will be processed much slower inside the loop:
- IS>( and DS<( (no matter if the following command is skipped or not).
- A lone If without an accompanying Then, but only when the condition is false (If with a true condition is unchanged).
If the condition of the If command can be either true or false (as in most actual cases), you should still add a closing parenthesis because the difference is so great.
This difference is responsible for the supposed difference between speed of If commands with or without an accompanying Then. There is actually no difference when you account for this factor—the commands are the same speed if you close the parenthesis.
An example use of this optimization:
:For(I,1,1200
:If not(fPart(I/5:Disp I
:End
should instead be
:For(I,1,1200)
:If not(fPart(I/5:Disp I
:End
Command Timings
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!
Error Conditions
- ERR:INCREMENT is thrown if the increment of the For( loop is 0.
- ERR:INVALID occurs if this statement is used outside a program.
- ERR:UNDEFINED is thrown if you DelVar the loop variable while inside the loop.
Related Commands
.
