Search code examples
variableslanguage-agnostic

What's the meaning of 'i' in for loop?


In almost every programming language, they use variable i when they explain for loop. Like,

for i in 'string':
  print(i)

or

for(var i ; i<100 ; i++) {
  console.log(i);
}

etc...

Does it mean anything? Or just a variable with no meaning?

I couldn't find any information about this in google search.


Solution

  • You have given two different example.

    For the first one

    for i in 'string': print(i)

    For this one, the variable 'i' is the variable where the value will be put from your parameter (here 'string'). If i gave an array as parameter for instance, each value of array will be put in 'i', step by step (element [0], then element[1], etc...).
    Note that this is a simplified view of this question and it doesn't work exactly like that for the program. But you can understand it as it.

    For the second one

    for(var i ; i<100 ; i++) {
      console.log(i);
    }
    

    You declare explicitly a variable to be used. This declared will be used by the loop, incrementig by the step you had defined until it reaches limit you also defined.

    It is slightly the same thing for both example. Hope i explain well