Blocks

  1. A compound statement is a group of consecutive statements enclosed within a pair of braces { }.

  2. A block is a compound statement that contains variable declarations.

  3. Variables declared within a block are local to the block (they have the block as their scope) and therefore these names can be used outside the block for something else.

  4. If an identifier is declared as a variable in each of two blocks, one within the other, then these are two different variables with the same name. Changes made to one of these variables will have no effect on the other one.

  5. Consider the following code fragment:

    {
        int x = 1;
        cout < < x < < endl;
        {
            cout < < x < < endl;
            int x = 2;
            cout < < x < < endl;
            {
                cout < < x < < endl;
                int x = 3;
                cout < < x < < endl;
            }
            cout < < x < < endl;
        }
        cout < < x < < endl;
    }

    The output is:

    1
    1
    2
    2
    3
    2
    1