The For( Command

Command Summary

Executes some commands many times, with a variable increasing from start to end by step, with the default value step=1.

Command Syntax


Menu Location

While editing a program press:

  1. PRGM to enter the PRGM menu
  2. 4 to choose For(, or use arrows
  3. 7 to choose End, or use arrows

Calculator Compatibility


Token Size

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. The syntax:


What the loop does:

  1. Stores start to variable.
  2. If variable is greater than end (or less than, if step is negative), then the For( loop ends immediately.
  3. Runs the statement(s).
  4. 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:

:Disp A

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 the end value to the variable you used in the For( loop. For example:

<some code>
:If <condition for exiting out>

For( can also be used to create a delay:

//delays for about 0.5 second (83+) or 0.2 second (83+SE/84+/SE/CSE)

If X is end, the delay will be about X/1000 seconds for the TI-83/83+, and X/400 for other calculators.

Unlike delays that use rand, a For( loop delay can execute an animation or other code during the delay.

For( loops can be nested to execute code once for every combination of values of several variables. For example:

:(some code)

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:


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

:Disp "X
:Disp L₁

will output:

For( loops can also be used to exceed the normal overflow limit of $10^{100}$ for variables and computations. For example, utilizing the optional step argument,


the value of A will be 1.8E100, which is otherwise impossible to assign to a variable by normal means. One could then use A as the step value for a For( command,


which doubles the value of A (so 1.8E100 becomes 3.6E100). This process can be repeated until the "true" overflow limit is reached at $10^{128}$ (since the calculator stores the exponent as a signed 8-bit integer, ranging from -128 to 127).


The seq( command, or simple math, can often be used in place of the For( command when dealing with lists. For example:

//can be


//can be

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 when they are the first line of code in 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 false (as in most actual cases), you should add a closing parenthesis because the difference is so great.

An example use of this optimization:

:If 0

//should be
:If 0

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


Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Noncommercial 2.5 License.