Why does the following not give an error?
for (int i=0; i<10; ++i) // outer loop
{
for (int i=0; i<10;++i) // inner loop
{
//...do somethin
You are actually making a new variable with the same name as another variable. Since they are in different scopes this is allowed, and the variable in the inner scope "owns" the name. You will not be able to access the outer-scoped i
inside the inner scope.
The for loop declaration itself is part of the scope of the for loop, so counts as part of the inner-scope in the case of the second i
.