# What does the English word "for" exactly mean in "for" loops?

English is not my first language, but since the keywords in programming languages are English words, I usually find it easy to read source code as English sentences:

• `if (x > 10) f();` => "If variable `x` is greater than `10`, then call function `f`."
• `while (i < 10) ++i;` => "While variable `i` is less than `10`, increase `i` by `1`."

But how a `for` loop is supposed to be read?

• `for (i = 0; i < 10; ++i) f(i);` => ???

I mean, I know what a `for` loop is and how it works. My problem is only that I don't know what the English word "for" exactly means in `for` loops.

• It is code. It isn't English. So... they don't directly compare. Don't try to find a direct meaning. The creators of a language had a limited vocabulary to choose from and went for terse.
– Oded
Commented Oct 28, 2013 at 11:34
• You may read it as `for each`. So it would become `for each i, till i is less than 10, perform the following steps`. Commented Oct 28, 2013 at 11:36
• @Oded OK, but why do you think they chose this word, and not another? OK, it's short, but I don't think this explains it.
– kol
Commented Oct 28, 2013 at 11:39
• @kol `for each i` pretty much explains everything. Commented Oct 28, 2013 at 11:42
• It is confusing in C, and the keyword is there for historical reasons. In languages like Algol it reads much more naturally: "for an index variable blah-blah-blah in range blah-blah-blah do blah-blah-blah". In C it can be "for nothing do unconditionally something" which is confusing indeed. Commented Oct 28, 2013 at 11:46

For this particular case, it would be something like:

"For every i starting from 0 up to (but not including) 10, do f(i)"

It would have to be worded a bit differently if the numbers were doubles or the looping conditions were more complicated, but you shouldn't really worry if you can't find a really natural sounding translation to English because programming languages are only based on natural languages, or mathematical notation: `for i = 1,... 10` is something you write in math, which is much older than programming.

• +1 for the math reference. in math you define a function `for` values in a domain. the part between the parentheses is the domain of the function defined between the braces. an empty `for` has an infinite domain, thus running indefinitely. Commented Oct 28, 2013 at 12:08
• @devnull I like the domain explanation as an answer Commented Oct 28, 2013 at 20:16

It's a lot more confusing because you've written out a C-style FOR loop, which isn't really a FOR loop; it's a WHILE loop with weird syntax. The FOR loop was defined by ALGOL. Pascal and BASIC picked up the concept; C, unfortunately, borrowed the name but not the semantics. (The best indication of this is to look at the `for (;;)` construct; it's not possible to define a true FOR loop that has no range to iterate over.)

The idea is that you define a range of elements, and an operation to perform for each element. For example, in Pascal you would write your example loop as:

``````for i := 0 to 9 do
f(i);
``````

In BASIC it's similar:

``````for i = 0 to 9
f(i)
next i
``````

This is a lot more readable: for each element `i` from 0 to 9, do something with the value. Note that there is no loop-ending condition involving `i` being `< 10`; instead, the last value of the range (9) is specified.

In English we use `for` in contexts like "This is for children under 6 year of age". The `for` statement was adapted in structured programming in this context.

Read `for (i = 0; i < 10; ++i) f(i);` as `for (; i < 10;) f(i);` and it might be clearer. The for statement has three parameters: initialization, condition, update. In some languages, the parameters can be lists.

For make the looping conditions clearer than the equivalent `while loop`. If the body is long the increment may not be obvious.

`````` i = 0;
while ( i < 10 ) {
f(i);
i++;
}
``````

In the following version the increment is misplaced. The `for` construct doesn't allow the increment (update) to be misplaced in this manner.

`````` i = 0;
while ( i < 10 ) {
i++;
f(i);
}
``````

Some languages provide a `for each` statement which is used to iterate over sets.

For loops in C/C++ are very flexible, and as such, they don't translate directly to English very well. The idea behind the for loop is iteration, and thus you would start out describing a simple initializer/condition/increment expression list with `for each value of i`.

This is my flexible, very verbose English representation of your for loop expressions:

`for (i = 0; i < 10; ++i)`

For each value of i (initially 0), and while i is less than 10, iterate by incrementing i.

This representation allows you to adapt to more complicated expressions, such as:

``````int count = 10;
for (i = 0, j = count - 1; i < count; i++, j--) { //... }
``````

For each value of i (initially 0) and j (initially `count - 1`), and while i is less than `count`, iterate by incrementing i and decrementing j.

Or even:

``````vector<T> v;
for(vector<T>::iterator it = v.begin(); it != v.end(); ++it) { //... }
``````

For each value of `it` (initially the first element of v), and while `it` is not the last element of v, iterate by incrementing `it` to the next element of v.

• +1 Thank you, very clear explanations. I especially like the way you use the word "initially".
– kol
Commented Oct 28, 2013 at 20:36

The word "for" in English is a preposition, which usually precedes a noun (or sometimes a noun phrase functioning as a noun), e.g., "for Jesús", "in school", "around the tree", "into a dark, gloomy forest". [As a helpful aside, I was taught a preposition is generally any word you can use to fill in the '___' in the sentence, "The airplane flew ___ the cloud."]

Specifically, this particular preposition, "for", can be used in the context of "service", in the sense that person P Performs a Procedure "for" another person B, the Beneficiary, as in: "P made lunch 'for' B", meaning "P made lunch 'for the benefit of' B", or "P made lunch 'on behalf of' B".

So in the context of programming language's quasi-English origin, to give a more verbose example for Mason Wheeler's excellent explanation, consider:

A wealthy householder needs to tell the nanny to make sure that, as each of his seven children line up to walk out the door into the cold to school, she is to perform the appropriate nanny service 'for' each of them, to (a) retrieve the appropriate overcoat 'for' each one, and (b) hand each the brown-bag lunch she had made 'for' each one of them.

In pseudocode, to expand on Mason Wheeler's Pascal example, the father (programmer/code) instructs the nanny (function) thus [with commentary delimited by square brackets]:

[set, array, or range] ChildrenOfMine of [datatype] IndividualChild;

IndividualChild i in ChildrenOfMine;

for [each] i := [starting from] Youngest to [ending at] Eldest do [this service] fnNannyGrabCoat&LunchFor(i);

I hope this helps. I myself find that looking at the etymology/history/usage of a word helps nail down its meanings in its various applications.

Meriam Webster definition

For - "used to indicate ... something is going to or toward "

so your statement is i (starting at 0) going to 9.

• That is the meanng in a sentence such as "I bought this book for my sister". In this phrase, for is indeed used to indidcate that the book is going to(ward) my sister. There is no range here, certainly no loop. I don't think it is this meaning that is referred to in programming languages. Commented Oct 28, 2013 at 12:04
• The OP was looking at how the code statement could be interpreted. Most of the answers were you can't. I provided an alternative, a way it could be(loosely). Commented Oct 28, 2013 at 12:51
• I think the question was meaning to ask "Why is this word used", or "how should the word be interpretted so that it is consistant with English". You gave a literal interprettation that didn't answer the intent of the question.
– user69037
Commented Oct 29, 2013 at 22:53