5. Scope of variables #
Created Tuesday 22 October 2019
- C++ has an availability scope for each variable.
- Only one variable can be accessible by a name, anytime anywhere.
The following cases show all possible outcomes of a scope error:
- Variable are accessible only within their nearest container codeblock.
- init Variable in for loop are in scope inside the braces. Similarly for while, **switch **statements.
- Overriding of value: if a variable in declared in the same scope, then this is an invalid practice.
- Overriding of value: if a variable is declared in an **inner **scope, with the same name as a variable from an **outer **scope, then the outer variable is lost, i.e the **inner **one shadows the **outer **one.