4

Possible Duplicate:
Why are we using i as a counter in loops?
Why are variables “i” and “j” used for counters?

This may seems stupid, but why everybody use i (if i is already in use, then j) in for loop checking ?

Means:

for(int i = 1;i <= 5;i++){
 for(int j = 1;j <= i;j++){
 System.out.print(i);
}

Why i and j ? We can use first and second also ? Check this, all (9 out of 10) uses i, j. Why ? Any reason or just doing because everybody does that ?

Community
  • 1
  • 1
Arpssss
  • 3,760
  • 5
  • 35
  • 77

6 Answers6

14

This programming convention has been around for a long time, and probably goes back all the way to Fortran. In Fortran 77, variables beginning with the letters I, J, K, L, M, or N were taken to be of type INTEGER (unless explicitly declared otherwise). That made them very well suited to be loop variables.

Of course i, j etc have been used in maths to denote matrix/vector/summation indices for much, much longer than computers have existed.

NPE
  • 464,258
  • 100
  • 912
  • 987
3

Simple variable names that are easy to read. Also it dates back to the C days in K+R

SeanPONeil
  • 3,871
  • 4
  • 28
  • 42
3

I think it is a habit that finds it simple origin in the fact that i is the first letter of integer and index.

Martijn Courteaux
  • 65,802
  • 45
  • 192
  • 282
3

i and j are commonly used in linear algebra when doing matrix multiplication and summations. i is conveniently short for index. and j is lexicographically adjacent to i.

Now that it's so established, I wouldn't use anything else, lest the programming gods bring pain upon ye.

Travis Webb
  • 14,134
  • 6
  • 53
  • 104
3

i stands for index j comes after i..

so its easy to remember and handle

that's why we are using in sequence i,j,k,l.....

MAC
  • 15,659
  • 8
  • 53
  • 95
2

i, and j are also used in math and physics. i is often the notation for Cartesian x-axis basis unit vector while j is used for Cartesian y-axis basis unit vector.

wachpwnski
  • 678
  • 1
  • 5
  • 11