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.

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 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

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật