C++ FAQ Celebrating Twenty-One Years of the C++ FAQ!!!
(Click here for a personal note from Marshall Cline.)
Section 39:
[39.8] What are the C++ scoping rules for for loops?

Loop variables declared in the for statement proper are local to the loop body.

The following code used to be legal, but not any more, since i's scope is now inside the for loop only:

for (int i = 0; i < 10; ++i) {
  ...
  if ( /* something weird */ )
    break;
  ...
}

if (i != 10) {
  // We exited the loop early; handle this situation separately
  ...
}
If you're working with some old code that uses a for loop variable after the for loop, the compiler will (hopefully!) give you a warning or an error message such as "Variable i is not in scope".

Unfortunately there are cases when old code will compile cleanly, but will do something different — the wrong thing. For example, if the old code has a global variable i, the above code if (i != 10) silently change in meaning from the for loop variable i under the old rule to the global variable i under the current rule. This is not good. If you're concerned, you should check with your compiler to see if it has some option that forces it to use the old rules with your old code.

Note: You should avoid having the same variable name in nested scopes, such as a global i and a local i. In fact, you should avoid globals altogether whenever you can. If you abided by these coding standards in your old code, you won't be hurt by a lot of things, including the scoping rules for for loop variables.

Note: If your new code might get compiled with an old compiler, you might want to put {...} around the for loop to force even old compilers to scope the loop variable to the loop. And please try to avoid the temptation to use macros for this. Remember: macros are evil in 4 different ways: evil#1, evil#2, evil#3, and evil#4.