Trivia - In a For loop, why is I used? A trivia question (Yes, I know the answer) In a For loop, why is the variable I used as the counter? If you know the answer, wait a while before spilling the beans.  I would like to see the newbies take a whack at it. In various software programs, whether old or new, I have found this fact to be preponderous. Bryan Shaw Wednesday, March 26, 2003 You mean like: for(int i = 0; i < 10; i++) ? errr....because my finger was already over the "i" key when I typed "int"? Or has this gone way over my head? martin Wednesday, March 26, 2003 Because the word "iteration" starts with "i"? Alex Chernavsky Wednesday, March 26, 2003 Because in Fortran it is implicitly defined as integer? Alexei Wednesday, March 26, 2003 Good question! I never thought about it before. Now I do, I think Alexei is right. In FORTRAN it makes sense to use the shortest and simplest variable that you don't have to declare. My guess is early scientific programmers copied the habit in other languages and now its the norm. David Clayworth Wednesday, March 26, 2003 Is it i for index, a reference to the index registers that some processors would have (eg.  IX and IY on the Z80) GedB Wednesday, March 26, 2003 I don't know the answer to your question, but I know that the variables - i,j,k - are widely accepted as loop counters and indices.  (That is, they are not descriptive, but do the job.)  Maybe it was a shortened form for (Index) = I? In an older language like COBOL or FORTH.  That would be my guess. Dave B. Wednesday, March 26, 2003 i, j and k are often used in sigma notation aren't they? That's pretty much a for loop. That's probably why they ended up in fortran too. Tom Seddon Wednesday, March 26, 2003 In fortran 77  (and almost certainly Fortran IV and earlier) the variables i , j and k and implictly defined as integers precisely so they can be used as "throw away" counters in loops and leave your variable definitions free for variables that have significance for the problem in hand. I guess the tradition comes from the early Fortran coders who taught the next generation who taught the next... etc and passed on the tradition through the generations. Its never been challenged because its a) traditional, b)not harmfull, and c) so common that everyone expects i to be a loop counter. IIRC Fortran77 dosn't  actually have explicit for loops (it uses do while...)  so the tradition has migrated between language constructs over the years bil bil Wednesday, March 26, 2003 first guess: iteration. then after reading the posts about FORTRAN: Oh yeah, that might be the reason too. Prakash S Wednesday, March 26, 2003 It strange how people try so hard to get programmers to use some conventions, but fail, while other conventions succeed all by themselves. Ged Byrne Wednesday, March 26, 2003 bil has it right. The only other clairification is (and I'm trying to remember all of it), but in FORTRAN 77 variables i,j,k and I think through m, were automatically considered integers, unless you declared them differently. Andrew Brown Wednesday, March 26, 2003 ---"It strange how people try so hard to get programmers to use some conventions, but fail, while other conventions succeed all by themselves."---- The ones that succeed are normally those that nobody understands the reason for so they keep on.following them so they don't inadvertently break anything. There is an excellent anecdote about this in Primo Levi's "The Periodic Table". I won't tell you the title so you will have to go through the lot. You will find it well worthwhile; the book should be on any programmers "Desert Island" list Stephen Jones Wednesday, March 26, 2003 Well, FORTRAN was before my time, but today it seems to most commonly represent iteration or index. HeyCoolAid! Wednesday, March 26, 2003 In the ancient days of the Spectrum, many programmers used 'for f = 0 to n' instead, because pressing 'f' produced the 'for' keyword. Mr Jack Wednesday, March 26, 2003 Yes, I will cast another vote for the legacy influence of FORTRAN on naming of loop variables. When FORTRAN programmers moved to Pascal and C, the canonical names stuck. In FORTRAN, certain ranges of leading character in variable names "gave" the variable a data type by default.  I (letter) through N were "implicit" integers. I'm not certain you even had to declare I (for instance) if you used it in the program. There were other ranges of leading character defined that were floating point (I think). Bored Bystander Wednesday, March 26, 2003 I'm doubtful of the fortran explanation, because I just can't see that there were that many fortran programmers.  It sounds more like a cause than an effect. I'm sure it must go back to Assembler, which is the source of all. Ged Byrne Wednesday, March 26, 2003 I guess because in i, j ... m are commonly used as iterator variables in mathematics - e.g. series, sequences. Then Fortran brought this convention into the programming world (since Fortran stands for Formula Translator) Dino Dino Wednesday, March 26, 2003 In all versions of FORTRAN any variable that begins with I-N is an INTEGER, any variable that begins A-H or O-Z is a REAL. This behaviour can be changed with the IMPLICIT statement. Thus best practice in all FORTRANS up to F77 is to begin every program with:     IMPLICIT LOGICAL (A-Z) Best practice for F90 or F95 is:     IMPLICIT NONE This is not a programmers invention. This is an attempt to make engineers and scientist more comfortable by mimicing the mathematical conventions they used to. Look through hundred year old text books, they are full of things like:   m          n sigma( sigma ( f(a[i], b[j]) ) ) i = 1    j = 1 Anonymous Coward Wednesday, March 26, 2003 Once upon a time there were lots and lots of FORTRAN programmers. Way more than there were C programmers. Lots of us moved from FORTRAN to C over the couse of time. I once helped write a text retrieval system in FORTRAN; we chose the language because we could guarantee a standard FORTRAN compiler on every target platform (about 20 platforms back in those days), which was more than could be said for C. Of course we're talking 20 years ago here, so back before the dawn of recorded time. For the sake of historical accuracy, FORTRAN's main loop construct is called DO but it functions like FOR:       DO 100 I=1,10 C Do something with integers 1 to 10 100 CONTINUE Excuse me now, but I've come over all nostalgic and I have to see if I can find my ALGOL manuals. David Clayworth Wednesday, March 26, 2003 A quick google and I find that variables starting with I to N are implict integers and A to H and O to Z are reals unless explicitly declared otherwise at the top of the function so you could use i in a loop with declaring it. The behaviour can be turned off with the IMPLICIT NONE statement. Back when most programmers were mathmaticians and FORTRAN was king (and lower case was unknown) it took hold and has never been ousted because basically its easy, programmers take shortcuts all the time and this is a shortcut and convention in one. It might be interesting to see if that other granddaddy of programming languages lisp had its own convention or wether the use of recursion rather then loops meant that i never took hold in the first place bil bil Wednesday, March 26, 2003 i = Illuminati. In 1954 the original FORTRAN authors met in private with the Bilderberg group in Reading PA at a large farm house  owned by the late Senator of PA - George B. Stevenson. This meeting established the control of computing by the illuminati for years to come. Although the meetings were held in secret, as most Bilderberg meetings are, an insider later leaked information pertaining to the use of the letter i in FORTRAN and it's significance. Apparently when the i variables are parsed by the FORTRAN compiler, notification is sent to Xenu, the alien leader oif Scientologists, in his mountain fortress and he will raise the daily tally of "those controlled by the illuminati" in his excel spreadsheet. Really this means nothing but it serves it's purpose as column fodder at the yearly Illuminati meetings when members need to know exactly how many people are "under their control". trollbooth Wednesday, March 26, 2003 As Dino said (and Anonymous Coward repeated), the real origin is mathematics. Any computer program that uses the convention (FORTRAN, whatever) got the idea from math. Other math conventions: x, y, z are the variables you want to solve for; m or n are the number of items in a set or collection; and powers are usually a, b, or c. So the perhaps more interesting question is, why *weren't* these conventions [widely] adopted in the programming world? Martha Wednesday, March 26, 2003 Once upon a time someone put a poster in a University hallway reading:     "God Is Real". A few hours later an addition appeared: "(unless declared as integer)" Alexei Wednesday, March 26, 2003 "God is dead" - Nietzsche "Nietzsche is dead" - God Steven C. Wednesday, March 26, 2003 Wow, I am really surprised! One person had it right.  Dino gets the prize. It all started with mathematics.  I,J,K...M are generally accepted as iterators in math equations. When FORTRAN came along, they followed that convention by making those variables implicit integers.  Those programmers moved on to C, Pascal, COBOL, RPG, etc. Now, it is just an accepted standard.  Thanks to all of you for responding.  I had a lot of fun reading your answers. Bryan Shaw Wednesday, March 26, 2003 I was told I-N are ints in fortran because that's the first two letters in INteger.  So was it just a coincidence, or maybe that's also where the math notation came from? :) xyz Wednesday, March 26, 2003 "Jesus saves sinners" "And turns them in for valuable prizes" Philo Philo Wednesday, March 26, 2003 I still say it was the illuminati. trollbooth Wednesday, March 26, 2003 Yeah, the math notation came from FORTRAN you're all idiots Wednesday, March 26, 2003 No offense, but IMO, this is a stupid question, especially if someone claims that "knows" the answer. I never programmed in Fortan. My first languages were Pascal and C and nobody "taught" me to use i or x or c or n or whatever. I just used them because it was simpler then to use "index" or "counter" or whatever. danpop Wednesday, March 26, 2003 Bryan Shaw wrote: "It all started with mathematics.  I,J,K...M are generally accepted as iterators in math equations." Well, OK -- but that doesn't really answer the fundamental question.  It just pushes it back one level. Alex Chernavsky Wednesday, March 26, 2003 >> One person had it right.  Dino gets the prize. >> It all started with mathematics.  I,J,K...M are generally accepted as iterators in math equations. >> When FORTRAN came along, they followed that convention by making those variables implicit integers.  Those programmers moved on to C, Pascal, COBOL, RPG, etc. And I know what a sigma symbol in an equation 'means' in mathematics, too - I know that it wasn't invented in Excel. For my money, the votes for FORTRAN are equivalent to the explanation of math equation influence. What do you think that the name FORTRAN comes from? FORmula TRANslation. FORTRAN syntax was designed to be accessible to mathematicians. The language even includes built in array operators. Bored Bystander Wednesday, March 26, 2003 My first thought was that i,j,k might have been used by mathematicians to index sequences long before programming languages existed... But now I'm not so sure. Maybe a math history guru knows? Dan Maas Thursday, March 27, 2003 The use of i in mathematics definetely pre-dates it's use in programming. I've seen pre-war maths books using it. Mr Jack Thursday, March 27, 2003 "A trivia question (Yes, I know the answer) In a For loop, why is the variable I used as the counter?" No one really knows the answer as to why a variable is used in a 'for' loop or any loop for that matter.  To claim you know the answer is very fickle on your part. You can only speculate as to the reasons why someone would use 'I' for a counter.  How many programs used 'I' and how many programs used 'N' or 'A' or 'B'.  Who knows?  How many programs used variables with names like 'Counter'.  Who know, who cares. Some mathematicians may have used I and some may have used K and some may have used N, but that has nothing to do with what a programmer used.  Surely assembly language came before FORTRAN.  There are no I's in assembly. Unless there is sound evidence that proves your conclusion that programers use 'I' in for loops, you are just blowing smoke especially when you 'claim to know the answer.'  Obsurd. I have a trivia question for you.  ( I know the answer ) How do we establish world peace? Eat Lead Thursday, March 27, 2003 Eat lead: world peace? We already have world peace! Just not on this world... try Mars, maybe. Mike Swieton Thursday, March 27, 2003   Fog Creek Home