What is the deal with $i, $k, and $j?
Moderator: General Moderators
What is the deal with $i, $k, and $j?
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?
Here is the deal with $i, $k, and $j
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.)
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
I am sure I asked this question beforewaradmin 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.
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Re: Here is the deal with $i, $k, and $j
Ding! Ding! Ding!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.)
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.
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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.
(#10850)
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
Re: Here is the deal with $i, $k, and $j
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 tooAKA Panama Jack wrote:.... Using anything other than $i for various types of loops just seems well... unnatural.....
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
Re: Here is the deal with $i, $k, and $j
That's out of context.jmut wrote: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 tooAKA Panama Jack wrote:.... Using anything other than $i for various types of loops just seems well... unnatural.....
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];
}
}
?>- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
- MrPotatoes
- Forum Regular
- Posts: 617
- Joined: Wed May 24, 2006 6:42 am