while(true){
bool flag;
while(true){
if (conditions) {
flag=true;
break;
}
}
}
In this case, is the flag reset to false condition after it exits the inner while loop? It seems from the display of the console that it is still in true condition.
No, there is no “reset”. There is no magic whatsoever. In fact,
flagwill not even be magically initialized tofalsefor you, you’ll have to do it yourself.I think you’re thinking of classic examples of scope and shadowing:
But there is no magic here, either. There are two different variables
awhich happen to share a name.ain the inner block refers to the second integer. If you could refer to the first integer, you’d be reading a completely different integer.Here is some magic:
Since
yis automatic, it gets destroyed at the end of its scope. (So did the seconda, by the way, only there was no way to tell.) If it has a destructor, it will be called. If its destructor does something fancy such as “resetting some flag”, you’ll see the results. (Only not throughy, which will be gone.)The fact that the
{}have no if/while/function/etc. attached to them is irrelevant.