FOR (I=1951; I<=2020; I++)

The History of Computer Programming FOR Loops

by Joe Honton

Ernesto, the team's newest member, becomes an unwitting pawn in the foil between senior programmers Antoní and Ken over the use of FOR loops.

They met by chance at Starbucks and were both on their way back to the office. Ken found himself alone with Antoní. He ventured a topic, "So what do you think about JavaScript's iterable protocol?"

"Well," Antoní hesitated for a moment, "I suppose for collections that don't have a numeric index, it's a good solution."

"OK, but for regular arrays?" Ken was fishing. "I mean, using for..of syntax instead of plain-old for loops. I'm thinking about using iterables everywhere from now on."

"Of course I use the iterable protocol with ES6 maps," Antoní began, but he didn't want to get into an argument over shiny new toys, so he deflected the remark, "but I'm no expert on the matter."

Ken didn't press further, and they left it at that.

A few days later Antoní was accosted by Ernesto, who was looking perturbed. "What's up? Is something on your mind?"

"My code didn't pass review," Ernesto started, "Ken asked me to look into reworking one of my functions — which uses a for loop — to see if I could write it using for..of syntax instead. He wants us to do everything that way now."

"I can't fight your battles," Antoní replied, trying to stay neutral, but clearly understanding where this was heading. "Let me ask you this. Do you have a good technical reason why it should be one way or the other? If you don't, then why not try following his lead — who knows, maybe you'll learn something new."

Ernesto wasn't expecting that answer. He thought Antoní would be an ally. "I'll tell you now, it's just going to delay things, and take more of my time, only to end up accomplishing the same thing. No performance gain. Dubious safety benefits. Maybe even more maintenance issues down the road." He was ready to storm out.

Antoní sized up the moment, and took a different tack. "Let me tell you a story about FOR loops." He eased into it, attempting to calm the situation before it turned into something bigger. Here's the gist of it. . . .

In the first week of my first programming class, I learned about DO loops —

DO I = 1, 100, 2

I knew almost nothing about computers or computer languages, but this one little statement opened my eyes to all sorts of possibilities. This, of course, is FORTRAN. Later on, I got a job at a Fortune 500 company, where I coded the same functionality like this —


Guess what language that was? Common Business-Oriented Language, also known as COBOL. At the same time, outside of work, I built a microcomputer and programmed it using the BASIC language. Lo and behold the same pattern showed up there, only this time it wasn't DO or PERFORM, it was —

FOR I = 1 TO 100 STEP 2

That was my first exposure to the keyword FOR in a programming language. And I recently learned that its predecessor was the Algorithmic Language, usually referred to as ALGOL, which was the first language to have compound statements. ALGOL would have expressed the same thing as —

for i:=1 step 2 until 100

So where did the keyword for come from? As it turns out, from the Swiss mathematician Heinz Rutishauser, who in 1951 coined the term für as a keyword in his high-level compiler Superplan. Later, when Rutishauser developed ALGOL in 1958, he decided to anglicize it to "for". It's been with us ever since.

Each of those languages used different syntax, but each provided the same level of specificity. You could provide starting and ending values for the index, an increment factor, and the index variable name.

By the time I began programming, the index variable name, by convention, was always I, with J used for nested loops and K for doubly-nested loops.

The single letter I rather than INDEX because BASIC, in the beginning, only used enough memory for single letter variable names. And in FORTRAN integer variables had to begin with the letters I through M, because the letters AH and NZ were reserved for floating point numbers.

None of those languages were up to the challenge I had before me: to build a line-oriented text editor. That's when I encountered this little gem —

for (i=0; i < 100; i++) { ... }

This was the C language. And it was all there: the for keyword, the parentheses, the semicolons, the curly braces, the zero-based indexing, the three-part compound statement, and that gorgeous plus-plus operator! It was simply perfect.

In fact, it was so perfect that the next language paid homage to it. C++ kept every part of that brilliant gem intact, and even appropriated the operator for its namesake. Next came Java, which also stuck to precisely the same syntax. Then Perl. Then PHP.

Its beauty was in its versatility.

Just look at the incrementor. If you use i-- you have a way to step through in reverse — a critical requirement whenever you start removing things from an array. And you can just as easily target only the even/odd elements of an array simply by using i+=2.

Look at the evaluator. Instead of just halting after 100 iterations, you can halt based on a value that is changing, something like result > 0.0001. And you can halt on multiple conditions too, like (i < 100 && result > 0.0001).

Even the humble initiator. Instead of just i=0, you can add a comma and initialize another variable at the same time, so i=0, result=1.0

And beyond versatility, is its sheer elegance. When broken down into its parts, the equivalent expressions become unwieldy, and the three parts sometimes end up being widely separated from each other —

do {
while (i < 100);

Antoní wrapped up his soliloquy with heaps of praise, "In seven decades of computer programming languages, the for loop has never been beaten in versatility or elegance. It is consummate both in form and function."

Ernesto was unsure how to response. Antoní had made himself clear. But there was still the matter at hand. He left it unresolved as he raced off to catch the northbound 5:15.

The next morning Ernesto plucked up his courage to approach Ken, the catalyst for this whole turmoil. "So I've been mulling things over, and wanted to circle back to your code review."

"What? What are you referring to?"

"The stuff about for loops."

"Oh, that!" Ken waved it off, as if it was nothing. "I was just thinking out loud. Don't take it so seriously." He clearly had a change of heart. "There's no way we should stop using for loops, they'll always have a place in programming."

No minifig characters were harmed in the production of this Tangled Web Services episode.

Follow the adventures of Antoní, Bjørne, Clarissa, Devin, Ernesto, Ivana, Ken and the gang as Tangled Web Services boldly goes where tech has gone before.

FOR (I=1951; I<=2020; I++) — The History of Computer Programming FOR Loops