Why this code is not giving error?

Oct 24, 2014 at 8:34am
Why this code is not giving error? We cannot declare the same variable twice but here when the inner loop terminates and re-enters every time i is re-declared. ??Why is it not giving error??

1
2
3
4
5
6
7
8
9
10
11
12
13
  #include<iostream>
using namespace std;
int main()
{
for(int j=5;j>0;j--)
{
    for(int i=1;i<=5;i++)
    {
    }
}
    cout<<"hello";
    return 0;
}
Last edited on Oct 24, 2014 at 9:47am
Oct 24, 2014 at 8:43am
how does it ever get to the i loop? j start out > 0 therefore the outer loop should never execute.

but if it did... i is able to re-declare itself because its scope is limited to the for loop. once the loop finishes, it is destroyed, and thus can be redeclared when the loop starts again.
Oct 24, 2014 at 9:48am
@Esslercuffi, yeah I corrected what u said and I understood what u said. thanks!
Topic archived. No new replies allowed.