I've heard from countless places the same thing on the scope of variables: once you enter a sub-scope and leave that sub-scope, any variables created or changes made to existing variables are destroyed.
Is this not true? If it is, can somebody explain this?
int factorial = 1;
for (int x = 2; x <= num; ++x)
factorial = factorial * x;
cout << num <<"! = " << factorial <<"\n";
Num being defined anywhere earlier, of course. Since "factorial = factorial * x;" is in the scope of the for loop, how is it possible that its value is carried out of that loop?
Or are there certain rules about scopes that I have overlooked somehow?