Does the variable used for iteration in a foreach loop have a more specific term than simply 'the foreach variable?' Especially to differentiate it from the collection variable?

E.g. item in foreach (item in collection)

Edit: Loop variant was suggested (wiki):

In computer science, a loop variant is a mathematical function defined on the state space of a computer program whose value is monotonically decreased with respect to a (strict) well-founded relation by the iteration of a while loop under some invariant conditions, thereby ensuring its termination.

To me this sounds similar to 'iteration variable.' Is this strictly speaking correct? I would have assumed that 'loop variant' refers to the internal integer (or similar) that the collection enumerator uses for its internal state- not the variable that is set to the Current value on each iteration.

Also, an enumerator of a linked list or a dummy enumerator that never terminates do not have monotonically decreasing variables, while at the same time the foreach loop does have its item variable.

Edit 2: The motivation of this question is to find a descriptive name for a parameter in a bytecode emitting API. This parameter is a number that refers to the local that will be set on each generated foreach iteration. It's not an iterationLocal, that would be misleading. currentLocal may be the most helpful name.

  • @jnm2 while looking to write a good naming / terminology question, you might find some guidance from English.SE and modify the question to try to make use of what applies in their checklist here. – user40980 Apr 3 '14 at 14:25
  • @MichaelT Thanks, that's reassuring. I appear to have been up to standards. – jnm2 Apr 3 '14 at 15:13
  • 1
    @jnm2 as this has retriggered a debate in chat, you might want to look at On the troubles of naming and terminology where I've tried to point out the history and get an idea of where we (P.SE) should go from here. – user40980 Apr 3 '14 at 15:16
  • 1
    I've removed some of the comments discussing the question's topicality, they were obsolete now that @MichaelT started a discussion in Meta. If you wish to continue discussing the matter, please take it there. – yannis Apr 3 '14 at 15:38
  • 2
    The first word that came to mind was iteree, but that's not an English word. I still think it makes sense as jargon. – Thomas Eding Apr 3 '14 at 17:08

I believe the term you're looking for is the loop variant.


It's been a long time since college, but if I remember correctly, loops are composed of loop variants and loop invariants. Put simply, loop variants change in each iteration where as loop invariants do not. A relationship between the loop variants and loop invariants determine the termination condition of the loop.

In my view, for a foreach the loop invariant is the enumerable object itself and the loop variant is the variable which holds each value.

When looking under the hood at the implementation details, yes the invariant is one past the last item in the enumerable and the variant is the enumerator, but they are not represented in the foreach loop. In fact, lots of work has gone into hiding those details.

All you are left with in this loop construct is the variable and the container. It is appropriate to call the variable the variant and the container the invariant, because that is how they function within the context of the construct.

  • 3
    “loop variant” is a subtly different concept – in for (int i = 0; i < 42; i++) { T value = collection[i]; do_something(value) }, the i is a loop variant because its changing value will terminate the loop at some point. However, the question is asking what the iteration variable (in my example value) would be called – assuming my above code was the de-sugared form for for (T value in collection) { do_something(value) } – amon Apr 3 '14 at 15:15
  • @Jeffery You make a good point about hidden details. (I currently have you upvoted, btw.) – jnm2 Apr 3 '14 at 15:54
  • @amon I have two comments. 1) Foreach is a loop, there must be a variant and an invariant. Implied implementation details don't count, so either foreach is not a loop, or the two explicit pieces make the variant and invariant. 2) The identifier is the part that varies and that change directly results in the termination of the loop. The loop is defined to end after identifier has the value of the last item in the container. – Jeffery Thomas Apr 3 '14 at 17:41
  • 1
    A "loop invariant" is a predicate, aka a property that you can evaluate, which will always be true at the end of each iteration of the loop, but may be (temporarily) false during the iteration. A list of items to iterate over is not a loop invariant. Try reading here: inst.eecs.berkeley.edu/~cs61b/sp06/labs/s4-2-1 or here: cs.miami.edu/~burt/learning/Csc517.041/workbook-notes/… . – David K Apr 4 '14 at 13:16

I checked the C# 5.0 spec, but all it gave me was foreach ( type identifier in expr ) embedded-statement. While embedded-statement works well to identify the statement block of the foreach, I don't think identifier is specific enough to be helpful.

The IEnumerator interface calls it the Current item.

  • 3
    Although you have listed interesting data points, I don't think this answers the question. The question is not how the C# grammar refers to this syntax, nor how IEnumerator refers to the Current item, but: “Does the variable used for iteration in a foreach loop have a more specific term than simply ‘the foreach variable’? Especially to differentiate it from the collection variable?” If you can show that “no” is the answer, that would be ok (but you don't). Listing two terms that are not the answer (identifier and current), well, don't make for a good answer either. – amon Apr 3 '14 at 15:24
  • While @Jeffery has a good point, Current may be more descriptive of what the variable is used for. – jnm2 Apr 3 '14 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.