C Language Forbidden--goto sentence

Source: Internet
Author: User
Tags goto

The goto statement is an unconditional transfer statement that is used in the following format:
Goto statement label;
Where the label is a valid identifier, with a ":" (colon) that appears somewhere inside the function, after the goto statement is executed, the program jumps to the label and executes the subsequent statement.

In addition, the label must be in a function with the goto statement, but it may not be in a loop layer. Usually a goto statement is used with an IF condition statement, and when a condition is met, the program jumps to the label.

The goto statement is usually not used, mainly because it will make the program level unclear, and difficult to read, but in the multi-layered nesting exit, with a goto statement is more reasonable.

Most people learn c more or less when they read or hear the use of a goto statement, but at some point the goto is goto.

eg

#include <stdio.h>#include<stdint.h>#include<string.h>intMainvoid){     for(intI=0;i< -; i++)         for(intj=0;j< -; j + +)        {            if(i+j== -)            {                Gototest; }}test:printf ("test...\n"); return 0;}

This example is probably the most common jump out of the loop usage, because there is no break. But...

eg

#include <stdio.h>intMainvoid){    intA=2; if(a>2)    {                 for(intI=0;i<Ten; i++) {printf ("i=%d", i); if(i==5)                Gototest; }}test:printf ("\r\nyes");return 0;}

The first time you try to use Goto, there is a misunderstanding, that is, if you do not execute the Goto,goto tag will not execute?

I didn't think it was going to happen, but the Goto tab, which is part of the program, is executed even if the Goto,goto tag is not executed (like testing). This gives us the time to write the program with a hidden bug, do not think that Goto does not execute, the label will not be executed.

Summary:

When we use Goto to jump out of a loop or to process a critical code segment, the position of the Goto label must be the location of the exception handling, even if no goto is executed. But most of the time, a goto code snippet is a key code handler, and we don't want the code at the tag to be executed (like exception handling) without a goto, so we can do it in the following way:

eg

#include <stdio.h>intMainvoid){    intflag=0; intA=2; if(a>2)    {                 for(intI=0;i<Ten; i++) {printf ("i=%d", i); if(i==5) {flag=1; Gototest; }        }    }    if(flag==1) {test:printf ("\r\nyes"); }return 0;}

C language Forbidden--goto statement

Related Article

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.