Free Hosting

Blocks (compound statements) and local variables


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():
1
2
3
4
5
6
7
8
int main()
{ // start a block
 
    // multiple statements
    int nValue = 0;
    return 0;
 
} // end a block
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!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
 
int main()
{
    using namespace std;
    cout << "Enter a number: ";
    int nValue;
    cin >> nValue;
 
    if (nValue > 0)
    { // start of nested block
        cout << nValue << " is a positive number" << endl;
        cout << "Double this number is " << nValue * 2 << endl;
    } // end of nested block
}
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    using namespace std;
    cout << "Enter a number: ";
    int nValue;
    cin >> nValue;
 
    if (nValue > 0)
    {
        if (nValue < 10)
        {
            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:
1
2
3
4
5
6
7
8
9
10
int main()
{ // start main block
 
    int nValue = 5; // nValue created here
 
    double dValue = 4.0; // dValue created here
 
    return 0;
 
} // nValue and dValue destroyed here
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void someFunction()
{
    int nValue;
}
 
int main()
{
    // nValue can not be seen inside this function.
 
    someFunction();
 
    // nValue still can not be seen inside this function.
 
    return 0;
}
Variables declared inside nested blocks are destroyed as soon as the inner block ends:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    int nValue = 5;
 
    { // begin nested block
        double dValue = 4.0;
    } // dValue destroyed here
 
    // dValue can not be used here because it was already destroyed!
 
    return 0;
} // nValue destroyed here
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{ // start outer block
    using namespace std;
 
    int x = 5;
 
    { // start nested block
        int y = 7;
        // we can see both x and y from here
        cout << x << " + " << y << " = " << x + y;
    } // y destroyed here
 
    // y can not be used here because it was already destroyed!
 
    return 0;
} // x is destroyed here
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{ // outer block
    int nValue = 5;
 
    if (nValue >= 5)
    { // nested block
        int nValue = 10;
        // nValue now refers to the nested block nValue.
        // the outer block nValue is hidden
    } // nested nValue destroyed
 
    // nValue now refers to the outer block nValue
 
    return 0;
} // outer nValue destroyed
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:
1
2
3
4
5
6
7
8
9
10
int main()
{
    // do not declare y here
    {
        // y is only used inside this block, so declare it here
        int y = 5;
        cout << y;
    }
    // otherwise y could still be used here
}
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.

0 comments:

Blogger Template by Clairvo