Playing with Blocks

When we discussed the compound statement , all we said about it was that if we group some statements between braces, then they act as a single statement. Now it's time for "the rest of the story."

Variables Declared in Blocks

One of the additional features of blocks is that we can declare variables that are local to any block, not just the block that composes the body of a function. For example, in the function:
int f1(int x)
{
   int a;
   if(x < 10) {
      float a;
      ...
   }
   ...
   return a;
}

we have two different variables named a . Inside the if statement, all occurrences of a refer to the floating point a that is declared at the beginning of that block. The a which is being returned in the last line of the body is the integer a that is declared at the top of the function body.

We discuss this only to make you aware that it is possible and to recognize what's happening if you see it. We don't find it to be a particularly recommendable feature to use.

Block Structured Control Flow

The blocks also give us an important way to think about the control flow. Notice that any of the figures that we used to illustrate control flow structures can themselves have a box put around them and they can be used any place we have other boxes. One effect of this is that all control flow constructs are in a sense self-contained.

Other factors to notice include:

  1. Each block has one entrance at the top and one exit at the bottom.
  2. Once control enters at the top of a block, it continues within the block until it leaves at the bottom. Control never leaves and then returns to the block.
  3. The blocks can always be arranged so that the lines connecting them never cross each other.

That concludes Part 3. You may return to the index or move on to the next lesson .