[C ++ Basics] goto usage

Source: Internet
Author: User
Dome
# Include <windows. h> # include <stdio. h> int main () {int n = 7; number2: printf ("Hello world \ n"); If (n = 7) {n = 8; printf ("n = 7 start \ n"); goto number0; printf ("n = 7 end \ n ");} else {printf ("n = 8 start \ n"); goto number1; printf ("n = 8 end \ n");} number0: printf ("Hi number0 \ n"); goto number2; number1: printf ("Hi number1 \ n"); Number3: printf ("Number3 \ n "); system ("pause"); Return 0 ;}

Output result

Conclusion Analysis and advantages and disadvantages

The GOTO statement can be used to jump out of a deep nested loop.
The GOTO statement can jump back or forward and continue to execute.

Goto can only jump to the function body, but not to the function in vitro. That is, Goto has a local scope and needs to be in the same stack.

A goto statement label consists of a valid identifier and symbol ";". The identifier naming rule is the same as the variable name, that is, it consists of letters, numbers, and underscores, the first character must be a letter or underscore. After the GOTO statement is executed,ProgramIt jumps to the statement label and executes the subsequent statement.
Generally, the GOTO statement is used with the if Condition Statement. However, while the GOTO statement brings flexibility to the program, it also makes the program structure layers unclear and hard to read, therefore, use this statement properly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.