Variable 'i' : undeclared identifier

I'm really confused right now! Can please anyone help me understand the scope of variable, as we can see it's locally initialized variable then why does the complier says it's an uninitialized variable.

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    for (int i = 0; i < 5; ++i);
    cout<<i; // Complier gives error on compilation that i is an undeclared identifier!
    
    for (int i = 0; i < 5; ++i){
    //Same thing happens for this blocked of code!
    //I was expecting an increment of i up to 5!
    }
    cout<<i; // but it says the 'i' guy doesn't exist!
}


Please Explain! Thanks!
Line 3: Get rid if the ; That terminates the scope of the for loop making line 4 outside the scope of the for loop and therefore i is undefined.

Line 10: i is only defined within the curly braces (lines 7-9). Line 9 terminates the scope of the for loop.
Topic archived. No new replies allowed.