Is variable declared inside loop a local variable?

Sep 28, 2014 at 2:14am
I just wonder if I declared a variable inside loop like while and for, is this variable a local one inside this loop?

E.g.:

for (int i=0; i < 10; i++)
{
............
}

Can I still use this "i" after the loop?

Thanks.


Last edited on Sep 28, 2014 at 2:15am
Sep 28, 2014 at 2:30am
no its only available inside the curly brackets of the forloop
Oct 2, 2014 at 3:54am
Thanks for reply. Just have another question, if I code like this:

for (i=0; i < 10; i++)
{
int a =0;
...........
}

Does "a" be declared in each loop ?
Oct 2, 2014 at 5:20am
yes a gets initialized to zero each time that loop runs.
Whatever is declared in those for(){<here>} will run as many times as the loop runs

btw i is not initialized in that for loop. i must be init somewhere before or inside the () for that to compile
Last edited on Oct 2, 2014 at 5:22am
Topic archived. No new replies allowed.