In general there is no loop iterator. For example, a single iterator variable may be insufficient (e.g., an array can be traversed from both ends simultaneously), termination of a loop can depend on external data (e.g., user input) and there are useful infinite loops (where iterators are useless).
However some heuristics can be used to identify iterator expressions. For loops they would frequently express two properties: progress and termination. Progress is often related to an update of the same variable with an value that depends on this variable, for example:
i++; // For indexed structures
p = p -> next; // For linked structures
Termination is often related to a comparison of a progress expression to some constant (meaning "constant for the loop"), for example:
i <= n; // For scalar values
p == null; // For pointers
In general there could be several progress expressions and therefore several termination constants, and several progress variables can be used in a termination expression.
So, a basic strategy would be:
- Collect all variables from an exit condition of a loop.
- Check which of them are modified in a loop body with a dependency on themselves.
But keep in mind, that some loops have no progress expressions, some have no termination conditions, variable updates can be indirect and there is aliasing.