Consider the following program
#include <iostream> int main() { int done = 5; int howmany = 3; for ( done = 1; done < howmany; done++ ) { std::cout << done << std::endl; } std::cout << done << std::endl; return 0; }
Its output to the console
1 2 3
In this program, the done variable is declared in the function code block of main . First, this variable is initialized to 5
int done = 5;
and then in the for clause it is assigned the value 1
for ( done = 1; done < howmany; done++ ) ^^^^^^^^
After exiting the loop, this variable will have the value 3.
Now consider the following program
#include <iostream> int main() { int done = 5; int howmany = 3; for ( int done = 1; done < howmany; done++ ) { std::cout << done << std::endl; } std::cout << done << std::endl; return 0; }
Its output to the console
1 2 5
In this program, the for clause declares a variable called done , which is the same as the variable name declared in the main code block.
for ( int done = 1; done < howmany; done++ ) ^^^^^^^^^^^^
This variable in the for clause hides a variable of the same name with the same name, declared in an external block of code. Its scope is the body of a for loop. After exiting the loop, this variable ceases to exist.
From C ++ Standard (6.5.3 The for statement)
3 for the statement.
Therefore, the last line of the program output
5
already displays to the console the value of the done variable, which was declared before the loop in the code block of the main function
In general, a name declared in the inner declaration area hides the same name declared in the outer declaration area.
From standard C ++ (3.3.10 Name hiding)
1 A name can be hidden declare region or derived class (10.2).
Note that there is an important difference in the definition of a for clause between C ++ and C.
In C ++, the second part of the for clause, where the condition is written, can also be a declaration.
View the following demo.
#include <iostream> int main() { int done = 5; int howmany = 3; for ( ; int howmany = done; done-- ) { std::cout << done << std::endl; } std::cout << '\n' << howmany << std::endl; return 0; }
Its output to the console
5 4 3 2 1 3
At each iteration of this for loop
for ( ; int howmany = done; done-- ) ^^^^^^^^^^^^^^^^^^
A variable is declared howmany , which hides a variable with the same name, declared in the function main . The value assigned to this variable is converted to a boolean type. If it is 0, the condition will be false and the loop will exit.
After the loop, the value of the howmany variable declared in main is displayed.
Well, and finally, an example, when in a for clause in two parts at once variables are declared that hide the variables of the same name with the same names, declared in main . After exiting the loop, these variables cease to exist, and the variables declared in main become visible.
#include <iostream> int main() { int done = 10; int howmany = 20; for ( int done = 5; int howmany = done; done-- ) { std::cout << done << std::endl; } std::cout << '\n' << done << std::endl; std::cout << howmany << std::endl; return 0; }
The output of this program to the console
5 4 3 2 1 10 20
The for clause in this program can logically be represented as
{ int done = 5; Label_repeat: { int howmany = done; if ( howmany != 0 ) { std::cout << done << std::endl; done--; goto Label_repeat; } else { goto Label_exit; } } } Label_exit: //...