Scope of variables declared within a for loop
31 Oct 07, 2:47PM
Scope of variables declared within a for loop
When you declare a variable within a for loop, i.e.
for(int i=1;i<10;i++) { /* do something */ }The integer i, is not available for use after the loop. Some old style C++ compilers allow this variable to be use later, i.e.
for(int i=1;i<10;i++) .... for(i=1;i<10;i++) .... //<- this is badHowever even if your compiler does allow this, it should be disabled (through the compiler options) or not used. As it is no longer ANSI C compliant, future compilers will not support this feature and most already reject this code.