How is the goto statement used in C++?

In C++, the goto statement is used to unconditionally transfer control of the program to a specified label. It can be used to break out of multiple nested loops, jump to the end of a function, etc.

The syntax for using the “goto” statement is as follows:

goto label;
...
label:
  // 代码块

In the example above, the goto statement will transfer control to the label named “label”.

However, using goto statements is often considered a bad programming style as it can lead to programs becoming messy and difficult to maintain. Using structured control statements (such as if statements and loops) is usually a better choice.

bannerAds