# Why is "i" chosen in programming loops?

**URL:** <https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257>\
**Category:** Factual Questions\
**Created:** [February 1, 2014, 3:22am UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257 "2014-02-01T03:22:12Z")\
**Posts on this page:** 15\
**Page:** 3

<div class="post-metadata">

**Author:** ![Senegoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senegoid/32/6606_2.png) [@Senegoid](https://boards.straightdope.com/u/Senegoid)\
**Post date:** [February 1, 2014, 11:14pm UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/41 "2014-02-01T23:14:04Z")

</div>

> [@Chronos](#):
>
> Now combine this with a boss who determines whether the code is correct or not based on whether it produces correct results, and who knows what the correct results are because that’s what the code produces.

Well, that sounds like the Holy Grail of programming to me! 🙂 Such a program would necessarily be perfectly bug-free.

After all the years of effort and study that have gone into the theory of determining program correctness (halting problems and all that), it’s nice to see that the problem has finally been solved by such a seeming straightforward approach. It’s easy to miss the obvious, isn’t it? 😃

---

<div class="post-metadata">

**Author:** ![Itself](https://avatars.discourse-cdn.com/v4/letter/i/d07c76/32.png) [@Itself](https://boards.straightdope.com/u/Itself)\
**Post date:** [February 1, 2014, 11:23pm UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/42 "2014-02-01T23:23:17Z")

</div>

> [@Senegoid](#):
>
> We could do with a bit of programming history lessons here. […]  
> So, you think Hungarian Notation is perverted? You don’t know the half of it! Hungarian Notation, as it popularly became known, is indeed a perverted and garbled version of the scheme as Microsoft (and presumably Simonyi himself) originally meant it to be. For an interesting history of this, and an essay in general on using variable naming conventions to assist with software reliability, see [Making Wrong Code Look Wrong](http://www.joelonsoftware.com/articles/Wrong.html) by Joel Spolsky, May 11, 2005. His discussion about Hungarian Notation, and how it got perverted, begin about two-thirds of the way through the essay. Recommended reading!

Hungarian notation always annoyed me for exactly that reason. If you’re writing, say, C++ code to perform some sort of physics computation, then it’s not a bad idea to tag variables with their units: foo\_ft, bar\_kg, baz\_deg, etc. (An even better idea is to stick one set of units throughout: radians, basic metric units, etc., but you don’t always have that option.) The point is that those are all doubles (or floats, or whatever); the semantic meaning of those variables is completely unknown to the compiler. On the other hand, the compiler is aware of what the types of variables are, and the syntax makes it clear to the user; writing something like ppiFoo for a pointer to a pointer to an integer just makes code harder to read for no benefit.

Another thing that’s always bothered me is styles that go too far in avoiding simple problems. In C++, for example, you can happily define ints and pointers to ints on the same line: “int x, \*y;” is totally legal, for example. The (alleged) problem is that you can also write “int _x, y" or even "int_ x, y”, which may lead some people to think that x and y are both declared as pointers to ints. There are a couple of dealing with this: Do nothing, since it’s any one familiar with C to be reading the code will be aware of that issue; declare pointers to a type and actual variables of that type, to avoid confusion; and so forth. Some places take it too far and insist that every single variable be defined on its separate line. At that point, you’re not even solving the original problem anymore. Some companies seem to go through a cycle when they have no coding standards (or people who naturally decent code themselves) to push the product out the door as quickly as possible, then switch to overcompensating and mandate an unnecessarily strict set of conventions to try to fix the original problem. Long story short, I’m glad I’m not doing that sort of work anymore.

---

<div class="post-metadata">

**Author:** ![Canadjun](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@Canadjun](https://boards.straightdope.com/u/Canadjun)\
**Post date:** [February 1, 2014, 11:24pm UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/43 "2014-02-01T23:24:57Z")

</div>

> [@aceplace57](#):
>
> Back then all our programs were in uppercase. Our line printer only supported uppercase. Uppercase bands were cheaper than a printer band that supported upper and lower. I was also told the multicase bands would break quicker.

I fondly remember line printers. I wrapped many Christmas presents in old listings. Such fun watching the results of some poor student leaving all their books on top of a line printer, not expecting it to run out of paper at an inopportune moment (the whole lid would tilt up, dumping anything left on it on the floor). I used an uppercase/lowecase print chain (what you called the thing with all the letters) maybe 2 or 3 times; virtually everything I did was with a (default) uppercase only print chain.

---

<div class="post-metadata">

**Author:** ![Francis\_Vaughan](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/francis_vaughan/32/3093_2.png) [@Francis\_Vaughan](https://boards.straightdope.com/u/Francis_Vaughan)\
**Post date:** [February 2, 2014, 1:13am UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/44 "2014-02-02T01:13:54Z")

</div>

> [@Senegoid](#):
>
> That seemed, at least, like a plausible theory. This was, after all, the FORTRAN compiler for the CDC 6400/6600/7600 machines, where legend had it that the operating system itself was written an absolute machine language by [Seymour Cray](http://en.wikipedia.org/wiki/Seymour_Cray).

I very much doubt Seymor wrote it. He did hand craft the logic, and the history of getting the first 6600 production ready is interesting.

You should seek out the book, [A Few Good Men from Univac](http://www.amazon.com/Good-From-Univac-History-Computing/dp/0262620758). It is wonderful read, and covers the early history of CDC.

---

<div class="post-metadata">

**Author:** ![Senegoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senegoid/32/6606_2.png) [@Senegoid](https://boards.straightdope.com/u/Senegoid)\
**Post date:** [February 2, 2014, 2:23am UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/45 "2014-02-02T02:23:47Z")

</div>

> [@Francis\_Vaughan](#):
>
> I very much doubt Seymor wrote it. He did hand craft the logic, and the history of getting the first 6600 production ready is interesting.

What was plausible, though, is that _somebody_ wrote that compiler in machine language.

Although, even that was never quite plausible. It was a HUGE program, as you might expect of a compiler written in assembly language. But the coding style sure screamed “WRITTEN IN BARE BONES BINARY BY A TEAM OF 10,000 ELBONIANS AND TRANSLATED TO ASSEMBLY BY SOME AUTOMATED SYSTEM”.

---

<div class="post-metadata">

**Author:** ![Senegoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senegoid/32/6606_2.png) [@Senegoid](https://boards.straightdope.com/u/Senegoid)\
**Post date:** [February 2, 2014, 2:32am UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/46 "2014-02-02T02:32:47Z")

</div>

> [@leahcim](#):
>
> That is how I understand it, too. Mathematical formulas have some very old conventions:
> 
> [ul]  
> [li]a,b,c,… are constants.[/li][li]i,j,k,… are indices.[/li][li]n,m,l,… are “sizes of things”[/li][li]x,y,z,… are variables[/li][/ul]
> 
> FORTRAN conventions reflect the older math/physics usage. It’s called “FORmula TRANslation” for a reason.

There’s even some history behind this, or at least some of this.

The convention of using letters near the end of the alphabet for variables, and letters near the beginning of the alphabet for constants (as in, for example, the general quadratic equation in two variables: ax[sup]2[/sup] + bxy + cy[sup]2[/sup] + dx + ey + f = 0) was originated (as I recall reading it) by Descartes, who did his major work before FORTRAN was in widespread use.

---

<div class="post-metadata">

**Author:** ![usedtobe](https://avatars.discourse-cdn.com/v4/letter/u/ec9cab/32.png) [@usedtobe](https://boards.straightdope.com/u/usedtobe)\
**Post date:** [February 2, 2014, 2:51am UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/47 "2014-02-02T02:51:55Z")

</div>

One little thing about Hungarian Notation:

YOU’RE DOING IT BACKWARDS!!!

The data type belongs as a suffix -num, -idx, -dat NOT the prefix.

The prefix indicates its logical grouping pers- (personnel) rc- accounts receivable dda- demand deposit account.  
The practice in COBOL was to use a prefix indicating the record (row) on which the data was stored.

Much more sensible as a sort key - I may wish to see which fields of MY DATA this other person is using. I’d prefer to see that over “show me every date field” or “lets see all the indicies”.

And that COBOL non-index which looks like an index?

Only n very specific versions of PERFORM XXXXXXXX (thru XXXXXXXX) VARYING TERMS-INDX FROM +1 (BY N) UNTIL TERM\_INDX \>\* TERMS-TABLE-LIMIT.

You could equally write:

MOVE ‘N’ TO WS-TERMS-FND-SW.  
SET TERM\_INDX TO +1.

PERFORM 01220-FIND-TERMS UNTIL WS-TERMS-FND-SW = ‘Y’  
OR TERM\_INDX \> TERMS-TABLE-LIMIT.

01220-FIND-TERMS.  
IF TERMS-CODE (TERM-INDX) = AR-TERM-CODE  
MOVE ‘Y’ TO WS-TERMS-FND-SW  
END-IF  
SET TERM-INDX UP BY +1  
01220-EXIT. EXIT.

(\* - test is at the top - interview trivia question)

---

<div class="post-metadata">

**Author:** ![JacquesDeNoir](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@JacquesDeNoir](https://boards.straightdope.com/u/JacquesDeNoir)\
**Post date:** [February 2, 2014, 6:00am UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/48 "2014-02-02T06:00:04Z")

</div>

Hi all, been lurking for years, finally registered to post to this thread.

I Learned FORTRAN IV in 1967, as used on an IBM 360 Model 44, an odd mainframe especially geared towards scientific processing (as opposed to commercial data processing), with variable length floating point arithmetic available. Although we never used that advanced and specialized feature, one could define variable length operations, and the machine operator could turn a dial on the front of the CPU for how many digits would be employed!

But the point of my post concerns the debate about the use of the word ‘index’ for the controlling value of a do loop. It is simply a part of the FORTRAN language definition that the ‘variable’ in the statement

DO ‘statement#’ ‘variable’ = ‘startvalue’ ‘endvalue’ ‘increment’

is known as the loop index. I was so sure that I googled and the first 5 or 6 hits agreed, and I learned it’s called an index in Oracle looping, too.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [February 3, 2014, 4:34pm UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/49 "2014-02-03T16:34:00Z")

</div>

**Senegoid** , it’s also possible that the program was originally written in sane assembly language, then assembled, and then the original assembly language source was lost while the binary was retained, and then disassembled back. This would be similar to my old OCRed code, though pathological in different ways (the code you had would actually work just fine, but was probably a lot harder for a human to read).

---

<div class="post-metadata">

**Author:** ![SpyOne](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@SpyOne](https://boards.straightdope.com/u/SpyOne)\
**Post date:** [February 4, 2014, 3:44am UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/50 "2014-02-04T03:44:15Z")

</div>

When I was learning programming (over 20 years ago), the example of a loop designed to produce a delay used “i” as its variable. The only reason given was that it was something you were unlikely to use for something else, as the favorites were x, y, z, a, b, &c.  
Not liking that reason very much, I disregarded that advice.

I’ve never been a professional programmer, only writing for myself, so it has never been an issue.

Which is probably no help to you at all.

---

<div class="post-metadata">

**Author:** ![Senegoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senegoid/32/6606_2.png) [@Senegoid](https://boards.straightdope.com/u/Senegoid)\
**Post date:** [February 4, 2014, 7:34am UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/51 "2014-02-04T07:34:05Z")

</div>

> [@Chronos](#):
>
> **Senegoid** , it’s also possible that the program was originally written in sane assembly language, then assembled, and then the original assembly language source was lost while the binary was retained, and then disassembled back. This would be similar to my old OCRed code, though pathological in different ways (the code you had would actually work just fine, but was probably a lot harder for a human to read).

Not so sure. Going only by the use of statement labels A001, A002, . . . B001, B002, . . . that might suggest what you’re thinking.

But the code was also the most horrendous stew of spaghetti code. You could open to any page and start skimming the code, and you could hardly find three to five lines in a row anywhere that didn’t have some kind of conditional or unconditional branch to some place a dozen pages away or a hundred pages away.

So, your hypothesis (written in assembly language, source lost, binary disassembled) is plausible, except for the idea that it might have originally been written in _sane_ anything.

But hey, in assembly language, you can name your floats and ints _anything you want_ because there are no default naming conventions at all!

---

<div class="post-metadata">

**Author:** ![Francis\_Vaughan](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/francis_vaughan/32/3093_2.png) [@Francis\_Vaughan](https://boards.straightdope.com/u/Francis_Vaughan)\
**Post date:** [February 4, 2014, 8:39am UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/52 "2014-02-04T08:39:55Z")

</div>

A linker will often spray crud about like that, or many compilers themselves. Is it all possible that you have a disassembled object code from a compiled program?

What paradigm of compiler is it? Or given its vintage, does it have any sort of overarching design rationale or is it just ad-hoc? Any sort of table driven system can often be auto-generated and will end up a total disaster if you want to look at what it generates.

Even working through modern compiler output, once linked, it can end up an insane mess. Fortran, with its lack of recursion, might make writing for a very weird compiler. And compilers were most certianly written in Fortran. Even the first Pascal compiler was bootstrapped from a Fortran version.

---

<div class="post-metadata">

**Author:** ![JSexton](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@JSexton](https://boards.straightdope.com/u/JSexton)\
**Post date:** [February 4, 2014, 4:29pm UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/53 "2014-02-04T16:29:44Z")

</div>

I always assumed it was short for “increment”, which makes sense in context.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [February 4, 2014, 4:52pm UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/54 "2014-02-04T16:52:20Z")

</div>

No, the increment is the amount by which it increases. If you want your index to take the values 1,3,5,7… for some reason, then your increment is 2.

---

<div class="post-metadata">

**Author:** ![Senegoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senegoid/32/6606_2.png) [@Senegoid](https://boards.straightdope.com/u/Senegoid)\
**Post date:** [February 4, 2014, 8:45pm UTC](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257/55 "2014-02-04T20:45:28Z")

</div>

> [@Francis\_Vaughan](#):
>
> What paradigm of compiler is it? Or given its vintage, does it have any sort of overarching design rationale or is it just ad-hoc? Any sort of table driven system can often be auto-generated and will end up a total disaster if you want to look at what it generates.

Paradigm: Paleolithic spaghetti. Okay, maybe neolithic spaghetti. This was Control Data, circa late 1960’s (although I was involved in the early 1970’s). As far as I could tell, EVERYTHING was written in assembly language. Even the COBOL compiler was written in assembly language. The entire operating system was written in assembly language. I think their ALGOL compiler was written in assembly (and I had heard that it was a 17-pass compiler or something really absurd like that). Their major utility programs were written in assembly language. This was at UC Berkeley. We had the source code for all of that. CDC shipped source code with their systems (not sure if everybody got this, or only customers who paid extra). Software updates were shipped in the form of source code updates, which we applied and then re-assembled the whole source. Assembling the FORTRAN compiler could take a good hour of machine time on a CDC 6400. A large part of my job was applying those updates, reassembling, testing, and installing; and likewise for the runtime library (which at least consisted of, you know, _modules_ ).

[Previous page](https://boards.straightdope.com/t/why-is-i-chosen-in-programming-loops/680257.md?page=2)
