ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# C++ 中的`goto`语句 > 原文: [https://beginnersbook.com/2017/08/cpp-goto-statement/](https://beginnersbook.com/2017/08/cpp-goto-statement/) `goto`语句用于将程序的控制转移到给定标签。` goto`语句的语法如下所示: ```cpp goto label_name; ``` **程序结构:** ```cpp label1: ... ... goto label2; ... .. label2: ... ``` 在程序中我们有任意数量的`goto`和`label`语句,`goto`语句后跟一个标签名称,每当遇到`goto`语句时,程序的控制权就会跳转到`goto`语句中指定的标签。 > `goto`语句几乎从不在任何开发中使用,因为它们很复杂,使得程序的可读性更低,更容易出错。代替`goto`,你可以使用[`continue`](https://beginnersbook.com/2017/08/cpp-continue-statement/)和[`break`语句](https://beginnersbook.com/2017/08/cpp-break-statement/)。 ## C++ 中`goto`语句的示例 ```cpp #include <iostream> using namespace std; int main(){ int num; cout<<"Enter a number: "; cin>>num; if (num % 2==0){ goto print; } else { cout<<"Odd Number"; } print: cout<<"Even Number"; return 0; } ``` **输出:** ```cpp Enter a number: 42 Even Number ```