Blocks (compound statements)
A block of statements, also called a
compound statement, is a group of statements that is treated by the compiler as if it were a single statement. Blocks begin with a { symbol, end with a } symbol, and the statements to be executed are placed in between. Blocks can be used any place where a single statement is allowed.
You have already seen an example of a block when writing the function main():
Blocks can be nested inside of other blocks. As you have seen, the
if statement executes a single statement if the condition is true. However, because blocks can be used anywhere a single statement can, we can instead use a block of statements to make the
if statement execute multiple statements if the condition is true!
06 | cout << "Enter a number: " ; |
12 | cout << nValue << " is a positive number" << endl; |
13 | cout << "Double this number is " << nValue * 2 << endl; |
If the users enters the number 3, this program prints:
3 is a positive number
Double this number is 6
Note that both statements inside the nested block executed when the if statement is true!
It is even possible to put blocks inside of blocks inside of blocks:
04 | cout << "Enter a number: " ; |
12 | cout << nValue << " is between 0 and 10" << endl; |
There is no practical limit to how many nested blocks you can have. However, it is generally a good idea to try to keep the number of nested blocks to at most 3 (maybe 4) blocks deep. If your function has a need for more, it’s probably time to break your function into multiple smaller functions!
Local variables
A variable’s
scope determines who can see the variable, and how long it lives for. Variables declared inside a block are called
local variables, and local variables have
block scope (also called local scope). Variables with block scope can be accessed only within the block that they are declared in, and are destroyed as soon as the block ends. Consider this simple function:
Because nValue and dValue were declared inside the block that defines the main function, they are both destroyed when main() is finished executing.
Variables declared inside a block can only be seen within that block. Because each function has it’s own block, variables in one function can not be seen from another function:
Variables declared inside nested blocks are destroyed as soon as the inner block ends:
Nested blocks are considered part of the outer block in which they are defined. Consequently, variables declared in the outer block can be seen inside a nested block:
10 | cout << x << " + " << y << " = " << x + y; |
Note that variables inside nested blocks can have the same name as variable inside outer blocks. When this happens, the nested variable “hides” the outer variable:
This is generally something that should be avoided, as it is quite confusing!
Variables should be declared in the most limited scope in which they are used. For example, if a variable is only used within a nested block, it should be declared inside that nested block:
By limiting the scope of a variable, you reduce the complexity of the program because the number of active variables is reduced. Further, it makes it easier to see where variables are used. A variable declared inside a block can only be used within that block (or nested sub-blocks). This can make the program easier to understand.
Summary
Blocks allow multiple statements to be used wherever a single statement can normally be used.
Variables declared inside blocks are called local variables. These variables can only be accessed inside the block in which they are defined (or in a nested sub-block), and they are destroyed as soon as the block ends.
If a variable is only used in a single block, declare it within that block.
No comments:
Post a Comment