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 variablex
is greater than10
, then call functionf
.”while (i < 10) ++i;
=> “While variablei
is less than10
, increasei
by1
.”
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.
7
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.
2
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 thancount
, 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 whileit
is not the last element of v, iterate by incrementingit
to the next element of v.
1
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.
3