Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Using Labels with the goto Statement

The appearance of an identifier label in the source program declares a label. Only a goto statement can transfer control to an identifier label. The following code fragment illustrates use of the goto statement and an identifier label:

// labels_with_goto.cpp
// compile with: /EHsc
#include <iostream>
int main()
{
   using namespace std;
   goto Test2;

   Test1:
      cerr << "At Test1 label." << endl;
   Test2:
      cerr << "At Test2 label." << endl;
}

Output

At Test2 label.

A label cannot appear by itself but must always be attached to a statement. If a label is needed by itself, place a null statement after the label.

The label has function scope and cannot be redeclared within the function. However, the same name can be used as a label in different functions.

See Also

Labeled Statements

Show:
© 2015 Microsoft