Scoping rules tell you where a variable
is valid, where it is created, and where it gets destroyed (i.e., goes out of
scope). The
scope of a variable extends from the point where it is defined to the first
closing brace that matches the closest opening brace before the variable was
defined. That is, a scope is defined by its “nearest” set of braces.
To illustrate:
The example above shows when variables
are visible and when they are unavailable (that is, when they go out of
scope). A variable can be used only when inside its scope. Scopes can be
nested, indicated by matched pairs of braces inside other
matched pairs of braces. Nesting means that you can access a variable in a scope
that encloses the scope you are in. In the example above, the variable
scp1 is available inside all of the other scopes, while scp3 is
available only in the innermost
scope.