Declare a variable inside the for loop


C++ allows to declare a variable inside the for loop declaration. It's like  the variable being declared just before the loop:


#include <iostream.h>

void main ()
{
for (int i = 0; i < 4; i++)
{
cout << i << endl;
}

cout << "i contains: " << i << endl;

for (i = 0; i < 4; i++)
{
for (int i = 0; i < 4; i++) // we're between
{ // previous for's hooks
cout << i;
}
cout << endl;
}
}




Added on April 5, 2007 Comment
}