Transfers of Control

You can use the goto statement or a case label in a switch statement to specify a program that branches past an initializer. Such code is illegal unless the declaration that contains the initializer is in a block enclosed by the block in which the jump statement occurs.

The following example shows a loop that declares and initializes the objects total, ch, and i. There is also an erroneous goto statement that transfers control past an initializer.

// Read input until a nonnumeric character is entered.
while( 1 )
{
    int total = 0;

    char ch = _getch();

    if( ch >= '0' || ch <= '9' )
    {
        goto Label1;       // Error: transfers past initialization
                           //  of i.

        int i = ch - '0';
Label1:
        total += i;
    } // i would be destroyed here if the 
      //  goto error were not present.
    else
        // Break statement transfers control out of loop,
        //  destroying total and ch.
        break;
}

In the preceding example, the goto statement tries to transfer control past the initialization of i. However, if i were declared but not initialized, the transfer would be legal.

The objects total and ch, declared in the block that serves as the statement of the while statement, are destroyed when that block is exited using the break statement.