# 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
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`. 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
Oct 28, 2013 at 11:39
• @kol `for each i` pretty much explains everything. 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. 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. Oct 28, 2013 at 12:08
• @devnull I like the domain explanation as an answer 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.

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
Oct 28, 2013 at 20:36

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.

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. 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). 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
Oct 29, 2013 at 22:53