Why this" i" in every for loop (outer) and "j" in inner for loop?


One idea is that i is used for "index".

Answer1:
One major factor for such practice could be the resources available. Majority of programming books and online resources follow the same notation. They tend to use 'i' for outer and 'j' for the inner loop, irrespective of the programming language used. While starting to program, you can easily get used to what you see regularly.

Why is it 'i' and not 'a' or 'b'? Well, loops are normally used while iterating an array. The elements are accessed through their index. Instead of using the name 'index', I can simply use 'i'. What about 'j'? I really don't know. Let me guess. The letter 'j' follows 'i'.

Answer2:

i and j have been used in maths long before it was used in programming. It is also used in papers that predate higher level languages (remember seeing stuff like  "Xi,j").

Well when fortran was designed they apparently followed the same trend, that they assigned alphabets starting with i to n to integers and and all other would default to real number(floating point).

Most people seem to have seen little reason to change that. It's widely known and understood, and quite succinct. Every once in a while you see something written by some psychotic who thinks there's a real advantage to something like:

for (int outer_index_variable=0; outer_index_variable < 10; outer_index_variable++)  
 for (int inner_index_variable=0; inner_index_variable<10;        inner_index_variable++)   
      x[outer_index_variable][inner_index_variable] = 0;

Thankfully this is pretty rare though, and most style guides now point out that while long, descriptive variable names can be useful, you don't always need them, especially for something like this where the variable's scope is only a line or two of code.


2 comments:

We are here to listen you, Comment your valueable opinion...!!!