I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
Index » Programming » Tutorials » C & C++ » Basic »

Scope of variables declared within a for loop

will\′s Photo
31 Oct 07, 9:47AM
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 bad

However 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.
Currently you need to be logged in to leave a message.