# Thread: How is this possible?

1. ## How is this possible?

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?

2. Is this not true?
Yes:
Code:
```int x = 0;

if (true) {
x = 12345;
}

cout << x; // Prints 12345```
But that's not true if you define a new variable in the nested scope with the same name. Then the inner name hides the outer name and changes made are to the inner name, which is destroyed when execution leaves the nested scope:
Code:
```int x = 0;

if (true) {
int x = 12345;
}

cout << x; // Prints 0```

3. So only variables defined in that scope are destroyed? Is it true in all cases that a variable defined outside a scope and used inside a sub-scope will, after the sub-scope has been executed, retain the value that was set to it, if there was any change, in the sub-scope?

4. Is it true in all cases that a variable defined outside a scope and used inside a sub-scope will, after the sub-scope has been executed, retain the value that was set to it, if there was any change, in the sub-scope?
Well, technically, yes. But apparently no, because the sub scope could be function, and by default, C++ is pass-by-value. In that case you'd make a copy, edit the copy, and the changes made won't be mirrored in the calling function. You can use a reference or a pointer to fix that though.

5. Code:
```int factorial = 1;
for (int x = 2; x <= num; ++x)
factorial = factorial * x;
cout << num <<"! = " << factorial <<"\n";

//Since factorial is declared outside of the for-loop, it is still in scope here.
//It will stay in scope until a } is encountered, probably at the end of this function.
//This means any changes made to it are kept until a } is encountered.```

6. Okie dokie, thanks for your help

While you're here, could you explain if it is possible to and why using an increment or decrement operator would have a different effect on something than adding or subtracting 1 to or from it respectively?

7. it wouldn't have any different effect as an end result, if there is any difference it would be that ++ and -- compile to increment and decrement instructions. (most archs support them) instead of addition and subtraction instructions.

++ and -- only for convenience.

edit: There is one thing you can do, the pre-increment and decrement operators for example:
boat = theJunk[++i] ;
would first increment i then set boat to theJunk[i] while:
boat = theJunk[i++]
would set boat to theJunk[i] and then increment i

8. A decent optimising compiler would convert i = 1 + 1; to an INC op anyway.