Page 1 of 2
What is the deal with $i, $k, and $j?
Posted: Tue Oct 03, 2006 8:46 pm
by Luke
I have wondered for a while why counting variables like the ones used in loops are often named $i, $k, or $j... but especially $i. I've seen $i used in almost every example loop I've ever seen. Anybody know why?
Posted: Tue Oct 03, 2006 8:54 pm
by waradmin
My computer science professor told us that people commonly use "i" to represent an integer because it "just makes sense". He said i+1 or i++ is just like saying integer+1 so its easier for programmers to track their variables. And I do agree, i and integer just go together.
Posted: Tue Oct 03, 2006 9:15 pm
by Pyrite
Actually, I think the i really means iterator and is most commonly used in for loops.
Posted: Tue Oct 03, 2006 9:18 pm
by waradmin
Pyrite wrote:Actually, I think the i really means iterator and is most commonly used in for loops.
Your probably right, my professor has his own 'ideas' about a lot of stuff, yet it seems to make sense.
Here is the deal with $i, $k, and $j
Posted: Tue Oct 03, 2006 9:18 pm
by stats4096
In the old days of assembler programming there was something called an index register that was commonly used to address an array. The first high level language that implemented arrays was Fortran. In its design, I, J, and K were defaulted as integers (counting values) as an implementation of the index register. I was for Index. (J and K were added to make array addressing easier.)
Posted: Tue Oct 03, 2006 10:17 pm
by alex.barylski
waradmin wrote:My computer science professor told us that people commonly use "i" to represent an integer because it "just makes sense". He said i+1 or i++ is just like saying integer+1 so its easier for programmers to track their variables. And I do agree, i and integer just go together.
I am sure I asked this question before

Re: Here is the deal with $i, $k, and $j
Posted: Tue Oct 03, 2006 10:29 pm
by AKA Panama Jack
stats4096 wrote:In the old days of assembler programming there was something called an index register that was commonly used to address an array. The first high level language that implemented arrays was Fortran. In its design, I, J, and K were defaulted as integers (counting values) as an implementation of the index register. I was for Index. (J and K were added to make array addressing easier.)
Ding! Ding! Ding!
This person wins the prize.
I think you will find a lot of older programmers who migrated to other languages from the days when everything was written in assembly continued to use similar styles. I know it was for me. Using anything other than $i for various types of loops just seems well... unnatural.

And since the main loop usually started with $i any loops inside usually continued from that letter. Hence $j, $k, etc...
Many of those older programmers are also teaching programming so the convention is passed on to the students. That's kind of why it has never died out.
Posted: Tue Oct 03, 2006 11:45 pm
by Christopher
Well ... they don't quite win the prize. Using i, j, k and m, n goes back long before computers to summation notation in mathematics. The i is the first index of summation (hence where the name of the "index register" comes from). Typically the notation was something like i=m,n where m and n were the upper and lower bounds of the range to be summed. Fortran was obviously mathematically focused (Formula Tranlation) so they made i, j, k defined as integers by default for use in summation loops.
Posted: Wed Oct 04, 2006 12:01 am
by daedalus__
I don't have anything to add, but this is an interesting topic.
Posted: Wed Oct 04, 2006 12:47 am
by timvw
Imho the use of i, j and k is a pattern on it's own

Re: Here is the deal with $i, $k, and $j
Posted: Wed Oct 04, 2006 1:46 am
by jmut
AKA Panama Jack wrote:.... Using anything other than $i for various types of loops just seems well... unnatural.

....
Do not want to sound picky but...not quite. Having 3 nested loops can be quite hard to read but might be necessary to have. So using $var[$i][$j] does not exactly provide good code readability. I would rather use more descriptive names in such scenarios and believe it will come quite natural too

Posted: Wed Oct 04, 2006 3:24 am
by Maugrim_The_Reaper
It's a convention - most people when they see $i, $j or $k in a loop context know what's going on...
So which mathematician do we blame, arborint?
Re: Here is the deal with $i, $k, and $j
Posted: Wed Oct 04, 2006 3:32 am
by Jenk
jmut wrote:AKA Panama Jack wrote:.... Using anything other than $i for various types of loops just seems well... unnatural.

....
Do not want to sound picky but...not quite. Having 3 nested loops can be quite hard to read but might be necessary to have. So using $var[$i][$j] does not exactly provide good code readability. I would rather use more descriptive names in such scenarios and believe it will come quite natural too

That's out of context.
Code: Select all
<?php
for ($i = 0, $iCount = count($var); $i < $iCount; $i++)
{
for ($j = 0, $jCount < count($var[$i]); $j < $jCount; $j++)
{
echo $var[$i][$j];
}
}
?>
It's easy to identify which is in what loop. Even from your example, because I've used $i, $j, $k since I start programming, I could tell immediately what was going on anyway

Posted: Wed Oct 04, 2006 8:49 am
by n00b Saibot
from what I have know, arborint gives the correct explanation
Posted: Wed Oct 04, 2006 9:04 am
by MrPotatoes
in vector math i/j/k are vector directions/axis