在 goto 语句中使用标签

源程序中 identifier 标签的外观声明了一个标签。 仅 goto 语句可将控制转移到 identifier 标签。 以下代码片段阐释了 goto 语句和 identifier 标签的使用:

备注

标签无法独立出现,必须总是附加到语句。 如果标签需要独立出现,则必须在标签后放置一个 null 语句。

标签具有函数范围,并且不能在函数中重新声明。 但是,相同的名称可用作不同函数中的标签。

示例

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

   cout << "testing" << endl;

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

请参见

参考

标记语句