
For-loops use induction variables. Often these have the identifier i. This involves the concepts of affine expressions, array indexing, and strength reduction in the compiler.
This C# article looks at the int i induction variable. It covers compiler optimizations.
First the intention of this program text is to demonstrate how you can loop through elements in an array using an integer variable such as "int i". An array is created and then it is accessed in an iteration space with the for-loop.
You can model the accesses of the array with vector sets. This tells you how the data accesses depend on each other and can be used to optimize for parallelism. In this program, each element is accessed only once and in sequential order with the int i variable.
Program that uses int i and loop [C#]
using System;
class Program
{
static void Main()
{
// Create an array.
// ... It has ten elements.
int[] array = new int[10];
array[0] = 1;
array[9] = 1;
// Loop through ten integers with int i.
// ... This is an induction variable.
for (int i = 0; i < 10; i++)
{
int value = array[i]; // Affine index expression
Console.WriteLine("{0} {1}", i, value);
}
}
}
Output
0 1
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 1
Description. In this program text, the int i variable is termed an induction variable, meaning its value depends on the iteration space of the for-loop. The induction variable i, then, can be applied as an affine expression to index into the array of integers.
Affine expressions. An affine expression is one that is based on the induction variable in a loop. The array access in this program would multiply the induction variable by 4, which is the byte size of an integer. A compiler optimization then could apply strength reduction to translate this multiplication into a series of adds.

Tip: In compiler theory, arrays are heavily focused on for optimization opportunities as they are so frequently used with variables such as induction variables.
In this article, we looked at the int i induction variable in a C# program. The int i variable is the most classic name for an induction variable in a for-loop, and it typically will invoke certain affine expressions which can then be used with lower-level optimizations in the compilation steps.
Compiler Explanation