Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Two important aspects of a variable are its scope and lifetime. The scope of a variable refers to how the variable can be accessed from other code. The lifetime of a variable is the period of time when the variable is valid and available for use. A variable's scope and lifetime are determined by where it is declared and the access modifier used to declare it.
A code block is a set of grouped code statements. Examples of code blocks include code organized in if-else, do-loop, or for-next statements. Block-level scope is the narrowest scope a variable can have. A variable declared within a block of code is available only within the block it is declared. In the following code, the variable blockCount can only be accessed from inside the if block. Any attempt to access the variable outside the block will generate a compiler error.