Understanding: int "i" in for statements

Can someone in the simplest way please explain what exactly int "i" is doing in for statements. It just seems so abstract to me.

I'll leave some code below for you to expound upon.

 
for (int i = 0; i<temps.size(); ++i) sum += temps[i]; 


It seems to me that int "i" comes out of nowhere. Basically, I have a hard time trying to define it. I see it's defined at the beginning of the for statement, but still it seems to appear out of thin air.

That piece of code is in the context of the code below...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "std_lib_facilities.h"

int main(){
	vector<double>temps;
	double temp;
	while(cin>>temp)
		temps.push_back(temp); // put into the vector
		
	// compute mean temperature
	double sum = 0;
	for (int i = 0; i<temps.size(); ++i) sum += temps[i];
	cout << "Median temperature: " << sum/temps.size() << endl;
	
	// compute median temperature
	sort(temps.begin(),temps.end()); //sort temps
					// from beginning to end
	cout << "Median temperature: " <<temps[temps.size()/2] << endl;
}


Also, I'm asking a general question. If you could answer in that way it would be much appreciated.

I'm confused about the whole concept of int "i".
Let's simplify the problem:

1
2
for (int i = 0; i < 10; i++)
    cout << i << ' ';

i is what we call an index. The above code snippet prints

0 1 2 3 4 5 6 7 8 9

Indexes are mostly used when traversing arrays. For example, say you create an integer array of size 10 and want to initialize all its entries to 0. One way to achieve this (although not the best), is to do the following:

1
2
3
int array[10];
for (int i = 0; i < 10; i++)
    array[i] = 0;

This is shorthand notation for

1
2
3
4
5
6
7
8
9
10
array[0] = 0;
array[1] = 0;
array[2] = 0;
array[3] = 0;
array[4] = 0;
array[5] = 0;
array[6] = 0;
array[7] = 0;
array[8] = 0;
array[9] = 0;
Last edited on
i is used as an iterator. In your first for loop up there, you're setting up i as 0, then you're saying keep doing the following code as long as i is less than temps.size(), and then you're increasing i by one. So let's say that temps.size() returns a 10. and your loop just prints i:
1
2
for(int i = 0; i < 10; ++i)
        cout << i << endl;

You would get this output:
0
1
2
3
4
5
6
7
8
9

The reason that 10 isn't printed is because it's only going to work as long as i is less than ten. So you can see that basically you're using i to limit how many times your code in the loop executes.
1
2
for (int i = 0; i < 10; i++)
    cout << i << ' ';

Can be re-written as:
1
2
3
4
5
6
7
8
{
    int i = 0;
    while (i < 10);
    {
        cout << i << ' ';
        i++;
    }
}


The idea here is that we are declaring i and an integer. Hence int i. When we reach the end of the for statement, i goes out of scope and is discarded.

I'm guessing you're more used to the world of C. In C, i needs to be defined at the start of a function, but can be used as often as you like in as many for statements as you like. It only goes out of scope when you reach the end of your current function. In C++, you can declare variables at almost any point in a function, it does not need to be close to the beginning. It will go out of scope at the next } which could be in an if statement or inside a loop.

There are individual preferences as to which is nicer:
1
2
3
4
5
6
7
8
// in C
void someFunc()
{
    int i;

    for (i = 0; i < 10; i++)
        printf("%d ", i);
}
// in C++
void someFunc()
{
    for (int i = 0; i < 10; i++)
        cout << i << ' ';
}

Really, the only functional difference is that i goes out of scope at the end of the for loop in the example on the right. I (and most C++) people agree that the right-hand method is the way to go. A general rule is to reduce the scope of your variables as much as possible.

When working in C, I often see this:
1
2
3
4
5
6
7
8
9
10
11
12
13
void someFunc()
{
    for i; // reserved as an index for any for loop

    for (i = 0; i < 10; i++)
        do_something( i );

    for (i = 0; i < 10; i++)
        do_something_else( i );

    for (i = 0; i < 10; i++)
        do_another_thing( i );
}

I don't like this sharing because if someone meant for i to be saved and used later, then someone else has just over-written it. By declaring your own variable in a very small scope, you guarantee that you are not screwing around with any other logic, all of your variables are contained. If you just assume that you can use i, then you might be messing up another later piece of logic.
Last edited on
Actually, Stewbond, that's the C89 standard. The C99 standard adopted the idea of scoping in control structures instead of just functions, so you can now define a variable inside of the loop the same way C++ does.

Doesn't mean that people do it, though. Old habits and all that. Besides, I like the linux kernel coding style.
By the way, if your question was more about "what is a for loop?" and not "why is i declared in the for loop?", then check out this link:

http://www.cplusplus.com/doc/tutorial/control/
Besides,
1
2
double sum = 0.0;
for (int i = 0; i<temps.size(); ++i) sum += temps[i];

is equivalent to
double sum = std::accumulate( temps.begin(), temps.end(), 0.0 );
and to
1
2
double sum = 0.0;
for ( double d : temps ) sum += d;

and to
1
2
double sum = 0.0;
std::for_each( temps.begin(), temps.end(), [&sum](double d) { sum += d } );


Older C required the declaration of every variable at the beginning of a scope. C++ introduced the possibility to postpone the declaration to later within scope, in the middle of expressions. Now both languages allow declaration of variables in for loop, and that such variables leaves scope when the loop ends.

The fact that most code examples name the index/counter/iterator variable "i" is just a habit.
Thank you all for your help. It's greatly appreciated.

-Incline
Topic archived. No new replies allowed.