Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Section A.5. Exploring Variable Scope

A.5. Exploring Variable Scope

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.5.1. Block-Level Scope

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.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial