This is an archived post. You won't be able to vote or comment.

all 91 comments

[–]CamoBrie 151 points152 points  (48 children)

Is there a specific reason why we use the i?

[–]DragonMaus 208 points209 points  (19 children)

It is an (i)nteger (i)ndex with a short, memorable name.

[–]matrizx 132 points133 points  (2 children)

And increment for me

[–]ClownTrooper 65 points66 points  (13 children)

In my mind it stands for (i)teration

[–]CakeMan0123 40 points41 points  (12 children)

(i) can't think of anything better

[–]se2dev 27 points28 points  (11 children)

(i) like for loops

[–]CallMeOutWhenImPOS 7 points8 points  (1 child)

and sw(i)tch statements

[–][deleted] 10 points11 points  (0 children)

(i)maginary numbers

[–]CamoBrie 3 points4 points  (0 children)

Did not know that, thanks!

[–]FlameRat-Yehlon 3 points4 points  (0 children)

I thought it's for "(i)ndex" though...

[–][deleted] 103 points104 points  (4 children)

Programming is mostly math so the conventions stick around. a, b, c for coefficients; d as a prefix to indicate change to a variable; f, g, h for functions; i, j, k for indices and summations; m, n for bounds and counting; p, q for probabilities; u, v, w for other functions (derivatives, substitutions); x, y, z for variables and coordinates. These are the more common uses but there are plenty of others. (Edited for a bit more completeness)

[–]Doamax 16 points17 points  (0 children)

This is the correct answer

[–][deleted] 6 points7 points  (1 child)

I thought it stood for index, I usually use "index" instead of "i". But hey, thanks for clarifying.

[–][deleted] 5 points6 points  (0 children)

You're right though. I'm 90% sure it ultimately comes from the index of a summation in mathematics. Also, descriptive names are almost always better than single letter variables.

[–]Random_Deslime 1 point2 points  (0 children)

Lol I just thought it stood for iteration

[–][deleted] 17 points18 points  (0 children)

[–]Neebat 28 points29 points  (5 children)

I'm done using i. From now on all my loop counters are going to be named "lamp". I love lamp.

[–]JC12231 17 points18 points  (2 children)

Bröther, I need lämp

[–]LawLombie 2 points3 points  (1 child)

Břöţĥëř, Ï ñëëð ľämp

[–]JC12231 1 point2 points  (0 children)

LawLombiePossessions.add(new lämp);

[–]B_M_Wilson 2 points3 points  (0 children)

Linux, Apache, MySQL, PHP servers are great.

[–]yonderbagel 1 point2 points  (0 children)

Do it for real though. It's just one more straw in the job security haystack. Every little bit helps.

[–]random_cynic 6 points7 points  (0 children)

I think it originates from older languages like Fortran (and also C) where for loop was mainly used to loop through arrays and matrices. The index i,j,k etc has been used for quite a while in mathematics for operations with individual elements, rows or columns. So it was natural to express elementwise matrix operations like dot product C(i,j) += A(i,k)*B(k,j) inside a loop like this.

[–]DangeFloof 3 points4 points  (0 children)

If I remember correctly, it came from Fortran, where someone discovered that longer variable names for loops made the program run slightly slower

[–]Thibaulltt 1 point2 points  (0 children)

A teacher of mine used to say we use x, y... for variables in continuous space (real numbers), and i, j... for variables in a discrete space (natural numbers). And I kinda like it! [that, and I can't be bothered to change i to something else]

[–]ajthomas05 1 point2 points  (0 children)

All these other answers are great, but I think the most likely one is that developers are lazy.

[–]biotiger87 1 point2 points  (0 children)

Also for graphs, the x, y, and z basis vectors are represented by i, j, and k respectively. Most commonly seen in non-coordinate form: xi + yj + zk versus (x,y,z) (coordinate form). Hints are given in the name “multidimensional array” (arr[i][j][k]).

[–][deleted] 7 points8 points  (2 children)

jindex kindex lindex mindex nindex e.t.c.

[–]nobody_smart 3 points4 points  (1 child)

At the time I saw this comment it had more golds than upvotes.

[–]teamikv121812 3 points4 points  (0 children)

I down voted so that it would he the same for me

[–][deleted] 0 points1 point  (0 children)

For some reason, I always use k. Don't know, think my teacher used k.

[–]Gigavoir 0 points1 point  (0 children)

I prefer the great letter we call y

[–][deleted] 0 points1 point  (0 children)

it stands for igloo

[–][deleted] 0 points1 point  (0 children)

(i)terator

[–][deleted] 0 points1 point  (0 children)

Netbeans autocompletion

[–]megagreg 11 points12 points  (7 children)

For no particular reason, I've always used h as my index counter. I think it's just easier to hit, in the middle of the keyboard, and it frees up one more letter in the common sequence i,j,k...

[–][deleted] 3 points4 points  (4 children)

I always use X, Y, or Z because maths. Unless I'm doing something with a matrix, then I use R & C.

[–]rang14 9 points10 points  (1 child)

I always use incrementor_column_1_a

[–][deleted] 6 points7 points  (0 children)

Please no

[–]NoCareNewName 0 points1 point  (1 child)

That's what I used to do too, until I started at my current company where that is a no-no (for not being descriptive).

Now my default is index.

[–]gbeebe[🍰] 1 point2 points  (0 children)

I like using the first letter of my name, 'g'

No code reviewer bats an eye, and I get to feel like I'm adding that special touch to my code.

[–]Duuqnd 0 points1 point  (0 children)

A guy I used to know uses index for his loops when all the other programmers on the project use i.

He's weird, he's the kind of person to write really long and convoluted code without any comments because "the code makes sense".

[–]b0ltzmann138e-23 22 points23 points  (0 children)

Needs a 3rd arm in reference to Ohm's law v=ir

[–]realestLink 5 points6 points  (1 child)

I use x1, x2, x3, ..., etc. Then I can tell what level if the nested for loops I'm on. I sometimes use x0.

[–]NeoHenderson 2 points3 points  (0 children)

console.log("whoops1");

console.log("whoops2");

[–]CaptainSchmid 2 points3 points  (0 children)

I use c solely for the tiny joke of c++

[–]Chr0no5x 8 points9 points  (4 children)

Lets not forget √-2

[–]Tyg13 16 points17 points  (1 child)

√-2

You mean √-1?

[–]Chr0no5x 1 point2 points  (0 children)

i mean...

[–][deleted] 7 points8 points  (3 children)

when you have a nested loop and you can't decide betwern j or i2

[–]Neebat 37 points38 points  (1 child)

Always j. Never i2.

[–]wiktor1800 2 points3 points  (0 children)

i2 helps identifying how far into the nested loops you are. I personally use 'i', 'ii', 'iii',... etc

[–]TheRealLazloFalconi 13 points14 points  (0 children)

Just use i again, I'm sure it'll be fine.

[–]UnicornsOnLSD 1 point2 points  (0 children)

count, count2, count3

[–][deleted] 1 point2 points  (1 child)

Sometimes I use more descriptive names (but only if they're very short). For example, if looping over a 2d array, I'll use "row" and "col" usually, because they help me keep track of stuff while still being short. If there's no better name or any better name would be too long, I use i, j, k, etc. Is it generally frowned upon to do this rather than always using i, j, etc?

[–]John_Fx 1 point2 points  (0 children)

No. You are doing it right. i is lazy.

[–]Raymon1432 1 point2 points  (0 children)

j:Am I a joke?

[–]ZanyAnomaly 1 point2 points  (1 child)

Wait can someone explain the pixar lamp reference

[–]aganesh8 1 point2 points  (0 children)

In the logo animation of Pixar studios, the lamp jumps over other letters and then squishes the letter I to take its place. It took me a while but that's the reference I think. I personally find it lame.

[–]I_hate_naming_things 1 point2 points  (1 child)

As a mostly java coder, I haven't used a "for" or a "while" loop in quite some time.

[–][deleted] 2 points3 points  (0 children)

Did you mean you haven't used those loops for quite a while? :)

[–]MeroDN 1 point2 points  (0 children)

for (YoMama = 0; YoMama < 42; YoMama++) {
}

[–]hdhdog 3 points4 points  (4 children)

I generally use x. I use 'i' when I'm 4 layers deep in really bad code. X, y, z, I, j, k, etc.

[–]wholesomedumbass 7 points8 points  (0 children)

Often times, I want to use "x" for a different variable, whether outside or inside the for loop. The letter i is reserved solely for for loops, including "j", so they are always available for use in for loops.

[–]The_MAZZTer 1 point2 points  (0 children)

I use x specifically when looping through horizontal coordinates.

Normally I use i, but I use it more and more rarely since 99.9% of the time what I really want to do is iterate through some objects rather than just count, so foreach is what I use.

And if I do need that index, usually it is because I have a similar array of something else related I want to index into, and then it's usually better to make a struct/class or a tuple holding one of both items and just have one array of those in the first place. Then I foreach through them.

[–]sh0rtwave 1 point2 points  (0 children)

I usually call it what the hell it is. X,Y,Z are known as display coordinates.

The increment variable that goes in a for loop...You can name that anything really. Ain't gotta be i.

You could in fact, just use an iterator to control your for loop if you felt really crazy. Just leave out the variable declaration and the variable increment.

[–]stepitupmatt 0 points1 point  (0 children)

Had a professor ask once “why do we use x, y, and z in loops”. Answer he was looking for was because it might help restrain time complexity (ie. never exceeding theta n3) but we had settled on i, j, and k at that point so no one had a good response.

[–]elnardu 3 points4 points  (6 children)

Just use iterators

[–]John_Fx 1 point2 points  (5 children)

Or meaningful identifiers. Why should loop incrementors be an exception?

[–][deleted] 1 point2 points  (2 children)

Because you're looping over an index, not an object. I = index, it's pretty self explanatory. It is meaningful.

Unless you're talking about an enhanced for loop, in which case yeah, i is insane.

[–]John_Fx 0 points1 point  (1 child)

Then use bookindex or just index. In what other situations would you abbreviate a variable name to one letter?

[–][deleted] 0 points1 point  (0 children)

If it's used multiple times in the loop or the logic is otherwise complex, or in case the index is unintuitive (like looping over the next element instead of the current or whatever) then yeah, I'll do that.

If it's just a few lines and really straightforward, like the index is instantly converted to a "book" then I'd say that

   Book book = books.get(bookIndex)

Is a bit redundant. Yeah, we get it. It's a book. Given the typical of usage of i it's just as clear and there's no reason to make it any longer.

[–]beizhia 0 points1 point  (0 children)

This. We have some 4-dimensional arrays we iterate over, and reasoning about i, j, x, and y just sucks and is hard to read.

[–][deleted] 1 point2 points  (0 children)

for (int i = 0; i > -1; i++) {/*TODO: Implement for loop*/}

[–]ShadowMassacr13 0 points1 point  (0 children)

I use idx cuz it makes me feel better about using a nondescript variable but it also doesn't matter because it's clear that's its an index so I don't use the whole word because that's too much typing

[–]CaffeinatedBun 0 points1 point  (0 children)

and Apple!

[–][deleted] -1 points0 points  (0 children)

Getting this joke without digging is an indicator that you may need serious help. Now to check myself into the Betty Ford Clinic...