There's a for statement code and below is its output and description. I don't quite understand the description of the two declared cout. What does the author want to say? Thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
for (int count = 0; count < 10; count++) //first form
cout << count << “ “;
cout << “\n”;
int count;
for (count = 0; count < 10; count++) //second form
cout << count << “ “;
cout << “\n”;
int index = 0;
for (; index < 10; index++) //third form
cout << index << “ “;
cout << “\n”;
|
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
|
This example also illustrates another issue. Notice how count is
declared twice. This is legal here because of where the declarations are located. The second declaration is in the
local scope, as you’ve already seen, so the second count’s scope lasts until the end of the function or code block (to the end of this code snippet). However, the first declaration
is not local.Variables declared in initialization have a
restricted scope. These variables’ scopes last until the end of the for statement, which means that once the for statement ends, the variable(s) declared in initialization no longer exist. Thus, when the second declaration is encountered, there is no conflict because the first count no longer exists.