Hi everyone I'm a student who're learning computer science.
Now we're learning compiler theory and it tells us a word identifier then one day my roommate asked me this question.
We all know the statement
for(int i = 0; i < sum; ++i)
but what's the meaning of i?
I think it's short for integer but he thinks it stands for identifier.
I searched this on the web but found nothing..
Anyone have some ideas?
I thought it was a FORTRAN thing to use i, j and k as iterators (and extra letters if you need them, but you probably shouldn't be nesting your loops that deeply).
Edit: Also, I don't think it's intended to stand for anything.
Again, while you can make it into an acronym if you want, I don't think that's the point. c is for celeritas, does that mean i (the imaginary unit, defined as the square root of -1) stands for imaginary?
In your example, i is simply a loop variable of type int.
As chrisname alluded, the use of i as a loop variable goes back to the early days of Fortran where variable names beginning with i,j,k,l,m,n are implicitly integers.
In C++, iter and citer are also commonly used to indicate an iterator and const iterators of arbitrary user types.
typedef map<mytype> my_map_t;
my_map_t my_map;
my_map_t::const_iterator citer;
for (citer=my_map.begin(); citer!=my_map.end(); citer++)
{ // citer points to one instance of mytype in the map collection
cout << *citer;
}
I have read the first FORTRAN (it was all caps by then) manual and AbstractionAnon is correct. In the first version of FORTRAN, all integers had to have names that began with letters from i to n. That was the only way, as far as I know, to tell the compiler whether to treat the number as floating point or not. All other names were considered floating point.
Capitals are normally reserved for macros, global constants or values in enumerations; I'd use lowercase for all variables. Also, using capital I and L may be ambiguous if you did something like this:
1 2 3 4 5 6 7 8 9
for (int i = 0; i < 10; ++i) {
for (int l = 0; l < 10; ++l) {
for (int I = 0; I < 10; ++I) {
for (int L = 0; L < 10; ++L) {
my_stupidly_large_array[i][l][I][L] = 42;
}
}
}
}
Whereas it might be slightly less ambiguous to do,
1 2 3 4 5 6 7 8 9
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
for (int k = 0; k < 10; ++k) {
for (int l = 0; l < 10; ++l) {
my_stupidly_large_array[i][j][k][l] = 42;
}
}
}
}
Admittedly, you shouldn't be using loops nested four levels deep anyway, nor should you be using a 4-dimensional array of 104 = 10,000 elements...