search
HomeBackend DevelopmentC#.Net TutorialWhat is the meaning of goto statement in c language

In C language, the goto statement is called an unconditional transfer statement, which allows the control to be unconditionally transferred to a labeled statement within the same function; the syntax is "goto label;...label: statement;", where label can be any plain text except C keywords, and it can be set before or after the goto statement in the C program.

What is the meaning of goto statement in c language

The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.

C language goto statement

The goto statement is also called an unconditional transfer statement in C. It is said to be unconditional, but in fact it is still conditional, and the jump range is still Restricted, allows unconditional transfer of control to marked statements within the same function.

Grammar

The syntax of goto statement in C language:

goto label;
..
.
label: statement;

Here, label can be any plain text except C keyword, It can be set before or after the goto statement in the C program.

What is the meaning of goto statement in c language

Flowchart

What is the meaning of goto statement in c language

Usage

First, we need to determine Well, where we want to transfer, that is, the transfer end point, we need to set an identifier, that is, label (I am not showing English, when the goto statement makes an error, this word is likely to appear in the error content), and at the same time , we need to add a colon (:) after the label. In this way, we have set the identifier, and then we set the starting point - the goto identifier;. First we have to tell the computer that we are going to jump, which is the role of goto. Then we have to tell it where we want to jump, which is the identifier we just set. Finally, like other statements, we add points to it. (;) means the end of the statement. Here's what the editor page looks like.

What is the meaning of goto statement in c language

and the running results

What is the meaning of goto statement in c language

It can be seen that our printf in line 5 is not executed, and it goes directly to line 7 printf.

It can jump to the next statement or jump to the front.

What is the meaning of goto statement in c language

This way it becomes a simple loop with no end condition

What is the meaning of goto statement in c language

You can try running this For a piece of code, of course we can use a counter and add if judgment, give it an end condition, and let it have the ability of for and while.

Unfortunately, we cannot use goto across functions. There are other statements that can be implemented, so we will not go into details here.

What is the meaning of goto statement in c language

Here, if the if condition is true, after we output it, looking at the long string of uneven brackets below, I know what I want to do They set conditions there, and then break over and over again. It's annoying to think about it. How can I wait without goto at this time? Very soon! We directly goto and jump to

What is the meaning of goto statement in c language

to proceed to the next stage of our program.

But I wonder if you noticed that in the previous picture, we used two gotos, but they pointed to the same label. Yes, we can make the program jump at different starting points. to the same end point, but it should be noted that we cannot jump to two labels under the same goto. This is easy to understand. If it were you, receiving an order asking you to go to two or even more different places, You will also be confused, where should I go?

Another difference from before is that I added a semicolon (;) after the identifier xiayige:. Why is this? The identifier must be followed by a statement, which can be an assignment or a statement. But if there is really no way to add a statement after it, we can add a semicolon and treat it as an empty statement to make the identifier run.

Ps: In fact, you can declare a garbage variable here, or other meaningless statements, but it will make the code difficult to understand, and it is not recommended to do so.

This is what everyone does most with goto. As for other uses, it depends on your imagination. As the old saying goes, how bold a person is, how productive the land is.

The following is when I use goto.

#include<stdio.h>
int main ()
{
    printf("请输入要计算的算式,四则运算优先级一样高,从左到右依次计算\n");
    int jieguo=0,sz,gongju=0,gongju2=1;
    char ysf;
    scanf("%d",&jieguo);
    if (jieguo==0)
//直接输入等号的话%d似乎是0,有待商榷!!!!!!!!
//二次修改,if语句中判断量时只有0为假,除此之外的数字都表真
    {
    printf("**,你算**呢\n");
    goto chaojijieshu;
    gongju=1;
    gongju2=0;
    }
    else if (gongju2)
    
    {
    printf("请输入运算符\n");
    
    scanf(" %c",&ysf);
    //enter包含两个命令,算是两个字符\r和\n,后面的一个会占据scanf的输入位,所以要清空,或者用空格占位
    if(ysf==&#39;=&#39;)
    printf("**,你算**呢\n");
    }
loop:
    while(ysf!=&#39;=&#39;)
    {
        if (ysf==&#39;-&#39;)
        {
            printf("请输入数字\n");
            scanf("%d",&sz);
            jieguo-=sz;
            printf("请输入运算符\n");
            fflush(stdin);//清空标准输入流(stdin) fflush是stdio.h中的函数
            scanf("%c",&ysf);
            if(ysf==&#39;=&#39;)
            {
                goto jieshu;
            }
            else
            {
                goto loop;
            }
        }
        else if (ysf==&#39;+&#39;)
        {
            printf("请输入数字\n");
            scanf("%d",&sz);
            jieguo+=sz;
            printf("请输入运算符\n");
            fflush(stdin);//清空标准输入流(stdin) fflush是stdio.h中的函数
            scanf("%c",&ysf);
            if(ysf==&#39;=&#39;)
            {
                goto jieshu;
            }
            else
            {
                goto loop;
            }
        }
        else if(ysf==&#39;*&#39;)
        {
            printf("请输入数字\n");
            scanf("%d",&sz);
            jieguo*=sz;
            printf("请输入运算符\n");
            fflush(stdin);//清空标准输入流(stdin) fflush是stdio.h中的函数
            scanf("%c",&ysf);
            if(ysf==&#39;=&#39;)
            {
                goto jieshu;
            }
            else
            {
                goto loop;
            }        }
        else if(ysf==&#39;/&#39;)
        {
            printf("请输入数字\n");
            scanf("%d",&sz);
            while(sz==0)
            {
                printf("0能做分母吗?你算**呢\n");
            }
            jieguo/=sz;
            printf("请输入运算符\n");
            fflush(stdin);//清空标准输入流(stdin) fflush是stdio.h中的函数
            scanf("%c",&ysf);
            if(ysf==&#39;=&#39;)
            {
                goto jieshu;
            }
            else
            {
                goto loop;
            }
        }
    }
jieshu:
    printf("结果是%d\n",jieguo);
chaojijieshu:
    if (gongju)
    printf("真无语,重开吧\n");
    return 0;
    
}
//注意注意!!!!!!!!!!!!
//enter算是两个命令,所以有两个字符,在进行上一次输入之后
//在来一个enter,会占据scanf的字符位
//所以要清空标准输入流
//可以利用下述语句
//fflush(stdin);
//清空标准输入流(stdin) fflush是stdio.h中的函数
//以上是最好的解决办法
//初次之外,还有被称作偏方的办法
//二次修改 除此之外,而不是初次之外,打错字了
//如
//scanf(“ %c”,&ysf”)
//在scanf中加一个空格,可以顶掉enter多出来的一个字符
//还有
//加一句
//getchar()
//用getchar来捕捉多出来的\n

说明

在任何编程语言中,都不建议使用 goto 语句。因为它使得程序的控制流难以跟踪,使程序难以理解和难以修改。任何使用 goto 语句的程序可以改写成不需要使用 goto 语句的写法。

不推荐使用goto语句的原因:

①由于goto语句是无条件跳转指令,使用goto语句后回使得程序结构变得更加复杂,不利于以后代码维护,特别是需要交交接给被人维护的时候。

②由于目前编程采用的都是结构化编程,方便移植,而当采用goto语句后就会导致在结构化编程的项目中代码不便移植,因为如果使用goto从一个结构中跳转到另一个结构,而使得结构之间有了联系,不便移植。

③由于目前C语言等高级语言具有break、continue等语法,可以很好地代替goto,所以能不使用goto就不使用goto。

推荐使用goto的场景:

①从多重循环中直接跳出。

②出错时清除资源。

③可增加程序清晰度的情况。

使用goto函数的原则:

①使用goto语句只能goto到同一函数内,而不能从一个函数里goto到另外一个函数里。

②使用goto语句在同一函数内进行goto时,goto的起点应是函数内一段小功能的结束处,goto的目的label处应是函数内另外一段小功能的开始处,不能破坏功能的实现。

③不能从一段复杂的执行状态中的位置goto到另外一个位置,比如,从多重嵌套的循环判断中跳出去就是不允许的。 

④应该避免向两个方向跳转。这样最容易导致"面条代码",即逻辑混乱。

相关推荐:《C视频教程

The above is the detailed content of What is the meaning of goto statement in c language. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Developing with C# .NET: A Practical Guide and ExamplesDeveloping with C# .NET: A Practical Guide and ExamplesMay 12, 2025 am 12:16 AM

C# and .NET provide powerful features and an efficient development environment. 1) C# is a modern, object-oriented programming language that combines the power of C and the simplicity of Java. 2) The .NET framework is a platform for building and running applications, supporting multiple programming languages. 3) Classes and objects in C# are the core of object-oriented programming. Classes define data and behaviors, and objects are instances of classes. 4) The garbage collection mechanism of .NET automatically manages memory to simplify the work of developers. 5) C# and .NET provide powerful file operation functions, supporting synchronous and asynchronous programming. 6) Common errors can be solved through debugger, logging and exception handling. 7) Performance optimization and best practices include using StringBuild

C# .NET: Understanding the Microsoft .NET FrameworkC# .NET: Understanding the Microsoft .NET FrameworkMay 11, 2025 am 12:17 AM

.NETFramework is a cross-language, cross-platform development platform that provides a consistent programming model and a powerful runtime environment. 1) It consists of CLR and FCL, which manages memory and threads, and FCL provides pre-built functions. 2) Examples of usage include reading files and LINQ queries. 3) Common errors involve unhandled exceptions and memory leaks, and need to be resolved using debugging tools. 4) Performance optimization can be achieved through asynchronous programming and caching, and maintaining code readability and maintainability is the key.

The Longevity of C# .NET: Reasons for its Enduring PopularityThe Longevity of C# .NET: Reasons for its Enduring PopularityMay 10, 2025 am 12:12 AM

Reasons for C#.NET to remain lasting attractive include its excellent performance, rich ecosystem, strong community support and cross-platform development capabilities. 1) Excellent performance and is suitable for enterprise-level application and game development; 2) The .NET framework provides a wide range of class libraries and tools to support a variety of development fields; 3) It has an active developer community and rich learning resources; 4) .NETCore realizes cross-platform development and expands application scenarios.

Mastering C# .NET Design Patterns: From Singleton to Dependency InjectionMastering C# .NET Design Patterns: From Singleton to Dependency InjectionMay 09, 2025 am 12:15 AM

Design patterns in C#.NET include Singleton patterns and dependency injection. 1.Singleton mode ensures that there is only one instance of the class, which is suitable for scenarios where global access points are required, but attention should be paid to thread safety and abuse issues. 2. Dependency injection improves code flexibility and testability by injecting dependencies. It is often used for constructor injection, but it is necessary to avoid excessive use to increase complexity.

C# .NET in the Modern World: Applications and IndustriesC# .NET in the Modern World: Applications and IndustriesMay 08, 2025 am 12:08 AM

C#.NET is widely used in the modern world in the fields of game development, financial services, the Internet of Things and cloud computing. 1) In game development, use C# to program through the Unity engine. 2) In the field of financial services, C#.NET is used to develop high-performance trading systems and data analysis tools. 3) In terms of IoT and cloud computing, C#.NET provides support through Azure services to develop device control logic and data processing.

C# .NET Framework vs. .NET Core/5/6: What's the Difference?C# .NET Framework vs. .NET Core/5/6: What's the Difference?May 07, 2025 am 12:06 AM

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The Community of C# .NET Developers: Resources and SupportThe Community of C# .NET Developers: Resources and SupportMay 06, 2025 am 12:11 AM

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The C# .NET Advantage: Features, Benefits, and Use CasesThe C# .NET Advantage: Features, Benefits, and Use CasesMay 05, 2025 am 12:01 AM

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment