What is the deal with $i, $k, and $j?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

What is the deal with $i, $k, and $j?

Post 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?
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Actually, I think the i really means iterator and is most commonly used in for loops.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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.
stats4096
Forum Newbie
Posts: 1
Joined: Tue Oct 03, 2006 9:09 pm

Here is the deal with $i, $k, and $j

Post 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.)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :P
User avatar
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

Post 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. :D

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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I don't have anything to add, but this is an interesting topic.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Imho the use of i, j and k is a pattern on it's own ;)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: Here is the deal with $i, $k, and $j

Post 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 :)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Here is the deal with $i, $k, and $j

Post 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 :)
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

from what I have know, arborint gives the correct explanation
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

in vector math i/j/k are vector directions/axis
Post Reply